Delphi Nullable with Custom Managed Records
Published with special permission from Embarcadero - this post writes about pre-release Delphi version where everything is subject to change until finally released. Nullables are a rather simple concept. All you need is a value, and some flag that will tell you whether or not the value has been explicitly set or not. type TNullable<T> = record private FHasValue: boolean; FValue: T; function GetValue: T; procedure SetValue(AValue: T); public property HasValue: boolean read FHasValue; property Value: T read GetValue write SetValue; 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; Plain and simple. But then you have the fact that records are not automatically initialized and that your FHasValue bo