Skip to content

Commit

Permalink
Update Neon.Core.Nullables.pas
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsbejatto committed Dec 23, 2024
1 parent afe82c1 commit 0b0ed71
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions Source/Neon.Core.Nullables.pas
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ENullableException = class(Exception);
Nullable<T> = record
private
FValue: T;
FHasValue: string;
FHasValue: Boolean;
procedure Clear;
function GetValueType: PTypeInfo;
function GetValue: T;
Expand All @@ -49,6 +49,8 @@ Nullable<T> = record
function Equals(const Value: T): Boolean; overload;
function GetValueOrDefault: T; overload;
function GetValueOrDefault(const Default: T): T; overload;
function ToString: String;
function ToVariant: Variant;

property HasValue: Boolean read GetHasValue;
function IsNull: Boolean;
Expand Down Expand Up @@ -89,7 +91,7 @@ implementation
constructor Nullable<T>.Create(const Value: T);
begin
FValue := Value;
FHasValue := DefaultTrueBoolStr;
FHasValue := True;
end;

constructor Nullable<T>.Create(const Value: Variant);
Expand All @@ -103,7 +105,7 @@ constructor Nullable<T>.Create(const Value: Variant);
procedure Nullable<T>.Clear;
begin
FValue := Default(T);
FHasValue := '';
FHasValue := False;
end;

class operator Nullable<T>.Equal(const Left: Nullable<T>; Right: T): Boolean;
Expand Down Expand Up @@ -131,7 +133,7 @@ function Nullable<T>.Equals(const Value: Nullable<T>): Boolean;

function Nullable<T>.GetHasValue: Boolean;
begin
Result := FHasValue <> '';
Result := FHasValue;
end;

function Nullable<T>.GetValueType: PTypeInfo;
Expand Down Expand Up @@ -197,7 +199,7 @@ function Nullable<T>.GetValueOrDefault: T;

function Nullable<T>.IsNull: Boolean;
begin
Result := FHasValue = '';
Result := not FHasValue;
end;

class operator Nullable<T>.LessThan(const Left: Nullable<T>; Right: T): Boolean;
Expand Down Expand Up @@ -228,12 +230,42 @@ function Nullable<T>.IsNull: Boolean;
procedure Nullable<T>.SetValue(const AValue: T);
begin
FValue := AValue;
FHasValue := DefaultTrueBoolStr;
FHasValue := True;
end;

class operator Nullable<T>.Implicit(const Value: TValue): Nullable<T>;
begin
Result := Nullable<T>.Create(Value.AsType<T>);
end;

function Nullable<T>.ToString: String;
var
LValue: TValue;
begin
if HasValue then
begin
LValue := TValue.From<T>(FValue);
Result := LValue.ToString;
end
else
Result := 'Null';
end;

function Nullable<T>.ToVariant: Variant;
var
LValue: TValue;
begin
if HasValue then
begin
LValue := TValue.From<T>(FValue);
if LValue.IsType<Boolean> then
Result := LValue.AsBoolean
else
Result := LValue.AsVariant;
end
else
Result := Null;
end;


end.

0 comments on commit 0b0ed71

Please sign in to comment.