<div dir="ltr"><div><span style="font-family:arial,helvetica,sans-serif">Hello,<br><br></span></div><div><span style="font-family:arial,helvetica,sans-serif">In the code bellow, the generic type TNullableTyple is implemented (and incomplete for now).<br><br></span></div><div><span style="font-family:arial,helvetica,sans-serif">Is there any possibility of "nullable types" be added to RTL or anyother fpc provided package? <br></span></div><div><span style="font-family:monospace,monospace"></span></div><div><span style="font-family:monospace,monospace"><br><br>unit NullableTypes;<br><br>{$mode delphi}{$H+}<br><br>interface  <br><br>type<br><br>  { TNullable }<br><br>  TNullable<T> = record<br>  strict private<br>    FValue: T;<br>    FHasValue: IInterface;<br>    function GetValue: T;<br>    function GetHasValue: Boolean;<br>    procedure SetValue(const AValue: T);<br>    procedure SetFlatInterface(var Intf: IInterface);<br>  public<br>    constructor Create(const AValue: T);<br>    function GetValueOrDefault: T; overload;<br>    function GetValueOrDefault(Default: T): T; overload;<br>    property HasValue: Boolean read GetHasValue;<br>    property Value: T read GetValue;<br><br>    class operator Implicit(AValue: TNullable<T>): T;<br>    class operator Implicit(const AValue: T): TNullable<T>;<br>    class operator Explicit(AValue: TNullable<T>): T;<br>  end;<br><br>  TInteger = TNullable<Integer>;<br><br><br>function NopAddref(inst: Pointer): Integer; stdcall;<br>function NopRelease(inst: Pointer): Integer; stdcall;<br>function NopQueryInterface(inst: Pointer; const IID: TGUID; out Obj): HResult;<br><br>const<br>  FlagInterfaceVTable: array[0..2] of Pointer =<br>  (<br>    @NopQueryInterface,<br>    @NopAddref,<br>    @NopRelease<br>  );<br>  FlagInterfaceInstance: Pointer = @FlagInterfaceVTable;<br><br>implementation<br><br>uses<br>  SysUtils;<br><br><br><br>function NopAddref(inst: Pointer): Integer; stdcall;<br>begin<br>  Result := -1;<br>end;<br><br>function NopRelease(inst: Pointer): Integer; stdcall;<br>begin<br>  Result := -1;<br>end;<br><br>function NopQueryInterface(inst: Pointer; const IID: TGUID; out Obj): HResult;<br>stdcall;<br>begin<br>  Result := E_NOINTERFACE;<br>end;<br><br>{ TNullable }<br><br>procedure TNullable<T>.SetFlatInterface(var Intf: IInterface);<br>begin<br>  Intf := IInterface(@FlagInterfaceInstance);<br>end;<br><br>class operator TNullable<T>.Explicit(AValue: TNullable<T>): T;<br>begin<br>  Result := AValue.Value;<br>end;<br><br>function TNullable<T>.GetHasValue: Boolean;<br>begin<br>  Result := FHasValue <> nil;<br>end;<br><br>function TNullable<T>.GetValue: T;<br>begin<br>  if not HasValue then<br>    raise Exception.Create('Invalid operation, Nullable type has no value');<br>  Result := FValue;<br>end;<br><br>function TNullable<T>.GetValueOrDefault: T;<br>begin<br>  if HasValue then<br>    Result := FValue<br>  else<br>   Result := Default(T);<br>end;<br><br>function TNullable<T>.GetValueOrDefault(Default: T): T;<br>begin<br>  if not HasValue then<br>    Result := Default<br>  else<br>    Result := FValue;<br>end;<br><br>class operator TNullable<T>.Implicit(AValue: TNullable<T>): T;<br>begin<br>  Result := AValue.Value;<br>end;<br><br>class operator TNullable<T>.Implicit(const AValue: T): TNullable<T>;<br>begin<br>  Result := TNullable<T>.Create(AValue);<br>end;<br><br>procedure TNullable<T>.SetValue(const AValue: T);<br>begin<br>  FValue := AValue;<br>  SetFlatInterface(FHasValue);<br>end;<br><br>constructor TNullable<T>.Create(const AValue: T);<br>begin<br>  FValue := AValue;<br>  SetFlatInterface(FHasValue);<br>end;         <br><br></span></div><span style="font-family:monospace,monospace">end.</span><br></div>