Skip to content

TVoidResult

Ivan Semenkov edited this page Jan 27, 2021 · 1 revision

Table of contents

About

TVoidResult contains Ok flag or error type like in GO or Rust languages. It is a specialized TResult type with no value.

uses
  utils.result;
  
type
  generic TVoidResult<E> = class

Create

A new void result type can be created by using one of constructors.

CreateValue

Create new result contains Ok.

constructor CreateValue;
Example
uses
  utils.result;
  
type
  TSomeResult = {$IFDEF FPC}type specialize{$ENDIF} TVoidResult<String>;

var
  res : TSomeResult;
  
begin
  res := TSomeResult.CreateValue;
  
  FreeAndNil(res);
end;

CreateError

Create new result contains error.

constructor CreateError (AError : E);
Example
uses
  utils.result;
  
type
  TSomeResult = {$IFDEF FPC}type specialize{$ENDIF} TVoidResult<String>;

var
  res : TSomeResult;
  
begin
  res := TSomeResult.CreateError('something wrong');
  
  FreeAndNil(res);
end;

IsOk

Return true if result contains value.

function IsOk : Boolean;
Example
uses
  utils.result;
  
type
  TSomeResult = {$IFDEF FPC}type specialize{$ENDIF} TVoidResult<String>;

var
  res : TSomeResult;
  
begin
  res := TSomeResult.CreateValue;
  if res.IsOk then
    ;
  
  FreeAndNil(res);
end;

IsErr

Return true if result contains error.

function IsErr : Boolean;
Example
uses
  utils.result;
  
type
  TSomeResult = {$IFDEF FPC}type specialize{$ENDIF} TVoidResult<String>;

var
  res : TSomeResult;
  
begin
  res := TSomeResult.CreateError('something wrong');
  if res.IsErr then
    ;
  
  FreeAndNil(res);
end;

Error

Return error if not exists raise TErrorNotExistsException.

function Error : E;

TErrorNotExistsException

Raised when trying to get a error that does not exist.

TErrorNotExistsException = class(Exception)
Example
uses
  utils.result;
  
type
  TSomeResult = {$IFDEF FPC}type specialize{$ENDIF} TVoidResult<String>;

var
  res : TSomeResult;
  
begin
  res := TSomeResult.CreateError('something wrong');
  writeln(res.Error);
  
  FreeAndNil(res);
end;
Clone this wiki locally