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:
);
class
operator
True
(
Src: TMyRecord):
Boolean
False
operator OnesComplement(
Src: TMyRecord): TMyRecord;
end
TMyRecord
.
begin
Value := AValue;
operator TMyRecord
// Test something then... hardcode for now
Result := Src
Value =
5
// Test something, then... hardcode for now
Result :=
OnesComplement(
Result := Src;
var
MyRec: TMyRecord;
MyRec := TMyRecord
if
MyRec
then
WriteLn
'MyRec is true!'
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
MyTypeName
...
however, it's not clear why
not
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
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!