Difference between revisions of "Nullable types"

From Lazarus wiki
Jump to navigationJump to search
 
(3 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
Nullable types are types which can have no value (can be unassigned).
 
Nullable types are types which can have no value (can be unassigned).
One such type in Pascal is [[Pointer]] type which can have [[Nil|<syntaxhighlight lang="pascal" enclose="none">nil</syntaxhighlight>]] value which means that it isn't assigned to any specific address.
+
One such type in Pascal is [[Pointer]] type which can have [[Nil|<syntaxhighlight lang="pascal" inline>nil</syntaxhighlight>]] value which means that it isn't assigned to any specific address.
 
Same behavior can be implemented using [[Generics|generic types]] and advanced records with [[Operator overloading|operator overloading]].
 
Same behavior can be implemented using [[Generics|generic types]] and advanced records with [[Operator overloading|operator overloading]].
  
<syntaxhighlight lang="pascal">
+
The Nullable unit is part of FPC since 3.2.2 and its code can be seen in the GitLab repository: [https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/rtl-objpas/src/inc/nullable.pp nullable.pp].
unit Nullable;
 
 
 
{$mode delphi}{$H+}
 
 
 
interface
 
 
 
uses
 
  Classes, SysUtils;
 
 
 
type
 
  TNullable<T> = record
 
  private
 
    FHasValue: Boolean;
 
    FValue: T;
 
    function GetValue: T;
 
    procedure SetValue(AValue: T);
 
  public
 
    procedure Clear;
 
    property HasValue: Boolean read FHasValue;
 
    property Value: T read GetValue write SetValue;
 
    class operator Implicit(A: T): TNullable<T>;
 
    class operator Implicit(A: Pointer): TNullable<T>;
 
  end;
 
 
 
implementation
 
 
 
{ TNullable }
 
 
 
function TNullable<T>.GetValue: T;
 
begin
 
  if FHasValue then
 
    Result := FValue
 
  else
 
    raise Exception.Create('Variable has no value');
 
end;
 
 
 
procedure TNullable<T>.SetValue(AValue: T);
 
begin
 
  FValue := AValue;
 
  FHasValue := True;
 
end;
 
 
 
procedure TNullable<T>.Clear;
 
begin
 
  FHasValue := False;
 
end;
 
 
 
class operator TNullable<T>.Implicit(A: T): TNullable<T>;
 
begin
 
  Result.Value := A;
 
end;
 
 
 
class operator TNullable<T>.Implicit(A: Pointer): TNullable<T>;
 
begin
 
  if A = nil then Result.Clear
 
  else raise Exception.Create('Pointer value not allowed');
 
end;
 
 
 
end.
 
</syntaxhighlight>
 
  
 
Then you can define nullable types like:
 
Then you can define nullable types like:
Line 76: Line 16:
 
== See also ==
 
== See also ==
 
* [[How to use nullable types]]
 
* [[How to use nullable types]]
 +
 +
The standard nullable type in Object Pascal is Variant.
 +
* [[Variant]]
  
 
== External links ==
 
== External links ==
 +
* [https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/rtl-objpas/src/inc/nullable.pp FPC GitLab Repository nullable.pp]
  
 
* [https://www.arbinada.com/en/node/1439 Nullables in Free Pascal and Delphi]
 
* [https://www.arbinada.com/en/node/1439 Nullables in Free Pascal and Delphi]

Latest revision as of 17:19, 6 August 2022

English (en) suomi (fi)

Nullable types are types which can have no value (can be unassigned). One such type in Pascal is Pointer type which can have nil value which means that it isn't assigned to any specific address. Same behavior can be implemented using generic types and advanced records with operator overloading.

The Nullable unit is part of FPC since 3.2.2 and its code can be seen in the GitLab repository: nullable.pp.

Then you can define nullable types like:

NullableChar = TNullable<Char>;
NullableInteger = TNullable<Integer>;
NullableString = TNullable<string>;

See also

The standard nullable type in Object Pascal is Variant.

External links