About the author
Today, I've discovered new undocumented class operators in the Delphi 10.2 compiler.
Since class operators apply on records, these currently apply on records. But on platforms that support applying class operators on classes, they will apply to classes too.
The new class operators are True, False and OnesComplement, and requires the name of the type it's declared in, as a parameter.The True and False operators returns a Boolean, while the OnesComplement operator returns the type itself.
type TMyRecord = record Value: Integer; constructor Create(const AValue: Integer); class operator True(const Src: TMyRecord): Boolean; class operator False(const Src: TMyRecord): Boolean; class operator OnesComplement(const Src: TMyRecord): TMyRecord; end; constructor TMyRecord.Create(const AValue: Integer); begin Value := AValue; end; class operator TMyRecord.True(const Src: TMyRecord): Boolean; begin // Test something then... hardcode for now Result := Src.Value = 5; end; class operator TMyRecord.False(const Src: TMyRecord): Boolean; begin // Test something, then... hardcode for now Result := False; end; class operator TMyRecord.OnesComplement(const Src: TMyRecord): TMyRecord; begin Result := Src; end; var MyRec: TMyRecord; begin MyRec := TMyRecord.Create(5); if MyRec then WriteLn('MyRec is true!'); end.
From the above, you can see that you can treat a variable of the type as Boolean.
You can write a statement of the form
if MyTypeName then ...
however, it's not clear why
if not MyTypeName then ...
would produce the error "E2015 Operator not applicable to this operand type".
In addition, the parameter can be declared in 3 ways, decorated with const, var, ie one of the following
const Src: TMyRecord var Src: TMyRecord Src: TMyRecord
It's not clear how to invoke the OnesComplement operator, but it might be possible to invoke it in C++ using the ~ operator.
What you can do with them, is simply up to your imagination!
How to free more space on your home drive by redirecting the location for SDKs in RAD Studio
Learn the command line used to compile System.pas in Delphi
A method to design records so that they're allocated on a specific byte boundary, such as 16 bytes, 512 bytes, 4096 bytes, etc.
Learn why the map is cool in Go!