Implementation of Nullables for Delphi compatibility
Original Reporter info from Mantis: urhen
-
Reporter name: NoName
Original Reporter info from Mantis: urhen
- Reporter name: NoName
Description:
Available in Delphi 10.4, more infos: https://dalijap.blogspot.com/2020/05/delphi-nullable-with-custom-managed.html
Additional information:
type
TNullable<T> = record
private
FHasValue: boolean;
FValue: T;
function GetValue: T;
procedure SetValue(AValue: T);
public
class operator Initialize(out Dest: TNullable<T>);
property HasValue: boolean read FHasValue;
property Value: T read GetValue write SetValue;
end;
class operator TNullable<T>.Initialize(out Dest: TNullable<T>);
begin
Dest.FHasValue := False;
end;
function TNullable<T>.GetValue: T;
begin
if FHasValue then
Result := FValue
else
raise Exception.Create('Invalid operation, Nullable type has no value');
end;
procedure TNullable<T>.SetValue(AValue: T);
begin
FHasValue := True;
FValue := AValue;
end;