Posts

Showing posts with the label Sydney

Magic behind FreeAndNil

Delphi 10.4 Sydney brings one small but rather significant change. A change in the signature of the FreeAndNil procedure from procedure FreeAndNil(var Obj); inline; to procedure FreeAndNil(const [ref] Obj: TObject); inline; Wait, const means the variable is passed as a constant and cannot be changed from within the procedure. But the whole point of FreeAndNil is to change the passed object variable to nil. What kind of sorcery is this? Let's start from the beginning and the old FreeAndNil procedure, its purpose and implementation. The purpose of FreeAndNil is to simplify the following code sequence: Foo.Free; Foo := nil; While the actual implementation of FreeAndNil is slightly different, and it will first nil the reference and then Free the object instance (temporarily preserved in a local variable), that slight difference is not relevant for discussing the signature of FreeAndNil . procedure FreeAndNil(var Obj); var Temp: TObject; begin Temp := TOb...

Delphi Nullable with Custom Managed Records

Image
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...