OpOverloads2 (Delphi)
Contents
Description
This example shows how to write and invoke more class or record operator overloads. The strings added to the TMemo describe the code being executed and the operators that are actually used.
This example is being referenced from specific records like System.Variant that have many operator overloads, but the example shows you how to implement operator overloads in your own records.
Code
TMyRec = record
payload: Real;
class operator Equal(a, b: TMyRec) : Boolean;
class operator NotEqual(a, b: TMyRec) : Boolean;
class operator Trunc(a: TMyRec) : integer;
class operator Round(a: TMyRec) : integer;
class operator In(a, b: TMyRec) : Boolean;
class operator GreaterThan(a, b: TMyRec) : Boolean;
class operator GreaterThanOrEqual(a, b: TMyRec) : Boolean;
class operator LessThan(a, b: TMyRec) : Boolean;
class operator LessThanOrEqual(a, b: TMyRec) : Boolean;
class operator Multiply(a, b: TMyRec) : TMyRec;
class operator Divide(a, b: TMyRec) : TMyRec;
class operator IntDivide(a, b: TMyRec) : TMyRec;
class operator Modulus(a, b: TMyRec) : TMyRec;
class operator LeftShift(a, b: TMyRec) : TMyRec;
class operator RightShift(a, b: TMyRec) : TMyRec;
end;
class operator TMyRec.Equal(a, b: TMyRec) : Boolean;
begin
Form1.Memo1.Lines.Add('Equal(a, b: TMyRec) : Boolean;');
Result:= (a.payload = b.payload);
end;
class operator TMyRec.NotEqual(a, b: TMyRec) : Boolean;
begin
Form1.Memo1.Lines.Add('NotEqual(a, b: TMyRec) : Boolean;');
Result:= (a.payload <> b.payload);
end;
class operator TMyRec.Trunc(a: TMyRec) : integer;
begin
Form1.Memo1.Lines.Add('Trunc(a: TMyRec) : integer;');
Result:= Trunc(a.payload);
end;
class operator TMyRec.Round(a: TMyRec) : integer;
begin
Form1.Memo1.Lines.Add('Round(a: TMyRec) : integer;');
Result:= Round(a.payload);
end;
class operator TMyRec.In(a, b: TMyRec) : Boolean;
var
aint, bint: Integer;
begin
Form1.Memo1.Lines.Add('In(a, b: TMyRec) : Boolean;');
aint:= Trunc(a.payload);
bint:= Trunc(b.payload);
Result:= aint in [0..bint];
end;
class operator TMyRec.GreaterThan(a, b: TMyRec) : Boolean;
begin
Form1.Memo1.Lines.Add('GreaterThan(a, b: TMyRec) : Boolean;');
Result:= (a.payload > b.payload);
end;
class operator TMyRec.GreaterThanOrEqual(a, b: TMyRec) : Boolean;
begin
Form1.Memo1.Lines.Add('GreaterThan(a, b: TMyRec) : Boolean;');
Result:= (a.payload >= b.payload);
end;
class operator TMyRec.LessThan(a, b: TMyRec) : Boolean;
begin
Form1.Memo1.Lines.Add('LessThan(a, b: TMyRec) : Boolean;');
Result:= (a.payload < b.payload);
end;
class operator TMyRec.LessThanOrEqual(a, b: TMyRec) : Boolean;
begin
Form1.Memo1.Lines.Add('LessThanOrEqual(a, b: TMyRec) : Boolean;');
Result:= (a.payload <= b.payload);
end;
class operator TMyRec.Multiply(a, b: TMyRec) : TMyRec;
var c: TMyRec;
begin
Form1.Memo1.Lines.Add('Multiply(a, b: TMyRec) : TMyRec;');
c.payload := a.payload * b.payload;
Result:= c;
end;
class operator TMyRec.Divide(a, b: TMyRec) : TMyRec;
var c: TMyRec;
begin
Form1.Memo1.Lines.Add('Multiply(a, b: TMyRec) : TMyRec;');
c.payload := a.payload / b.payload;
Result:= c;
end;
class operator TMyRec.IntDivide(a, b: TMyRec) : TMyRec;
var c: TMyRec;
begin
Form1.Memo1.Lines.Add('IntDivide(a: TMyRec; b: integer) : TMyRec;');
c.payload := Round(a.payload) div Round(b.payload);
Result:= c;
end;
class operator TMyRec.Modulus(a, b: TMyRec) : TMyRec;
var c: TMyRec;
begin
Form1.Memo1.Lines.Add('Modulus(a: TMyRec; b: integer) : TMyRec;');
c.payload := Round(a.payload) div Round(b.payload);
Result:= c;
end;
class operator TMyRec.LeftShift(a, b: TMyRec) : TMyRec;
var c: TMyRec;
begin
Form1.Memo1.Lines.Add('LeftShift(a: TMyRec; b: integer) : TMyRec;');
c.payload := Round(a.payload) shl Round(b.payload);
Result:= c;
end;
class operator TMyRec.RightShift(a, b: TMyRec) : TMyRec;
var c: TMyRec;
begin
Form1.Memo1.Lines.Add('RightShift(a: TMyRec; b: integer) : TMyRec;');
c.payload := Round(a.payload) shr Round(b.payload);
Result:= c;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
x, y: TMyRec;
a, b: Double;
myint: Integer;
myBool: Boolean;
begin
x.payload:= StrToFloat(Edit15.Text);
y.payload:= StrToFloat(Edit16.Text);
Memo1.Lines.Add('Test: BoolToStr(x = y, True);');
Edit14.Text:= BoolToStr(x = y, True); // Calls TMyRec.Equal(a, b: TMyRec) : Boolean;
Memo1.Lines.Add('Test: BoolToStr(x <> y, True);');
Edit17.Text:= BoolToStr(x <> y, True); // Calls TMyRec.NotEqual(a, b: TMyRec) : Boolean;
Memo1.Lines.Add('Test: IntToStr(Trunc(x));');
Edit18.Text:= IntToStr(Trunc(x)); // Calls TMyRec.Trunc(a: TMyRec) : Integer;
Memo1.Lines.Add('Test: IntToStr(Round(x));');
Edit19.Text:= IntToStr(Round(x)); // Calls TMyRec.Round(a: TMyRec) : Integer;
Memo1.Lines.Add('Test: x in [0..y];');
if x in y then
Edit20.Text:= 'X is between 0 and ' + FloatToStr(y.payload) // Calls TMyRec.In(a, b: TMyRec) : Boolean;
else
Edit20.Text:= 'X is not between 0 and ' + FloatToStr(y.payload);
Memo1.Lines.Add('Test: BoolToStr(x > y, True);');
Edit21.Text:= BoolToStr(x > y, True); // Calls TMyRec.GreaterThan(a, b: TMyRec) : Boolean;
Memo1.Lines.Add('Test: BoolToStr(x >= y, True);');
Edit22.Text:= BoolToStr(x >= y, True); // Calls TMyRec.GreaterThanOrEqual(a, b: TMyRec) : Boolean;
Memo1.Lines.Add('Test: BoolToStr(x < y, True);');
Edit23.Text:= BoolToStr(x < y, True); // Calls TMyRec.LessThan(a, b: TMyRec) : Boolean;
Memo1.Lines.Add('Test: BoolToStr(x <= y, True);');
Edit24.Text:= BoolToStr(x <= y, True); // Calls TMyRec.LessThanOrEqual(a, b: TMyRec) : Boolean;
Memo1.Lines.Add('Test: x := x * y;');
x := x * y;
Edit25.Text:= FloatToStr(x.payload); // Calls TMyRec.Multiply(a, b: TMyRec) : TMyRec;
Memo1.Lines.Add('Test: x := x / y;');
x := x / y;
Edit26.Text:= FloatToStr(x.payload); // Calls TMyRec.Divide(a, b: TMyRec) : TMyRec;
Memo1.Lines.Add('Test: y := y div x;');
x := y div x;
Edit27.Text:= FloatToStr(x.payload); // Calls TMyRec.IntDivide(a, b: TMyRec) : TMyRec;
Memo1.Lines.Add('Test: x := 21 mod x;');
y.payload:= 21;
y := y mod x;
Edit28.Text:= FloatToStr(y.payload); // Calls TMyRec.Modulus(a, b: TMyRec) : TMyRec;
Memo1.Lines.Add('Test: y := y shl x;');
y := y shl x;
Edit29.Text:= FloatToStr(y.payload); // Calls TMyRec.RightShift(a, b: TMyRec) : TMyRec;
Memo1.Lines.Add('Test: y := y shr x;');
y := y shr x;
Edit30.Text:= FloatToStr(y.payload); // Calls TMyRec.LeftShift(a, b: TMyRec) : TMyRec;
end;
end.
Uses
- System.Variant.operator ! ( fr | de | ja )
- System.Variant.operator + ( fr | de | ja )
- System.Variant.operator - ( fr | de | ja )
- System.TimeSpan.TTimeSpan.operator Implicit ( fr | de | ja )
- System.TimeSpan.TTimeSpan.operator Explicit ( fr | de | ja )