Skip to content

Commit

Permalink
Added new DEMO NullableTypesShowcase
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Jul 28, 2024
1 parent 42e0bc1 commit 0f1e8f7
Show file tree
Hide file tree
Showing 2 changed files with 1,273 additions and 0 deletions.
55 changes: 55 additions & 0 deletions samples/nullable_types_showcase/NullableTypesShowcase.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
program NullableTypesShowcase;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
MVCFramework.Nullables;


procedure Main;
begin
var lNullableInt: NullableInt32;
Assert(lNullableInt.IsNull);
Assert(not lNullableInt.HasValue);
Assert(lNullableInt.ValueOrDefault = 0);
lNullableInt := 3; // compatible with base type
Assert(not lNullableInt.IsNull);
Assert(lNullableInt.HasValue);
Assert(lNullableInt.ValueOrDefault = 3);
Assert(lNullableInt.Value = 3);

var lVal: Integer;
if lNullableInt.TryHasValue(lVal) then
begin
// work with value...
{
TryHasValue is a shortcut for the following code:
if lNullableInt.HasValue then
begin
var lVal := lNullableInt.Value;
// work with value...
end;
}
end;

lVal := lNullableInt; // compatible with base type - equivalent to lNullableInt.Value
{
lNullableInt.Value raises exception if "lNullableInt.IsNull = True"
}
end;

begin
try
Main;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
end;
end;
Write('Press return to continue');
Readln;
end.
Loading

0 comments on commit 0f1e8f7

Please sign in to comment.