-
Notifications
You must be signed in to change notification settings - Fork 7
TVoidResult
Ivan Semenkov edited this page Jan 27, 2021
·
1 revision
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
A new void result type can be created by using one of constructors.
Create new result contains Ok.
constructor CreateValue;
uses
utils.result;
type
TSomeResult = {$IFDEF FPC}type specialize{$ENDIF} TVoidResult<String>;
var
res : TSomeResult;
begin
res := TSomeResult.CreateValue;
FreeAndNil(res);
end;
Create new result contains error.
constructor CreateError (AError : E);
uses
utils.result;
type
TSomeResult = {$IFDEF FPC}type specialize{$ENDIF} TVoidResult<String>;
var
res : TSomeResult;
begin
res := TSomeResult.CreateError('something wrong');
FreeAndNil(res);
end;
Return true if result contains value.
function IsOk : Boolean;
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;
Return true if result contains error.
function IsErr : Boolean;
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;
Return error if not exists raise TErrorNotExistsException.
function Error : E;
Raised when trying to get a error that does not exist.
TErrorNotExistsException = class(Exception)
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;