Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Forked from superswanman/Project1.dpr
Created November 29, 2016 19:41
Show Gist options
  • Save jpluimers/0c695e236fbf8356b443c2435700180c to your computer and use it in GitHub Desktop.
Save jpluimers/0c695e236fbf8356b443c2435700180c to your computer and use it in GitHub Desktop.
Use operator overloading for classes with non-ARC compiler
program Project1;
{$APPTYPE CONSOLE}
uses
System.SysUtils, System.Classes, System.Types, Vcl.Dialogs;
type
TStringListEx = class(TStringList)
public
// class operator In(const A: string; B: TStringListEx): Boolean;
class function &&op_In(const A: string; B: TStringListEx): Boolean; static;
end;
TPointHelper = record helper for TPoint // Possible in class helper!
public
// class operator Equal(const A: TPoint; const B: string) : Boolean;
class function &&op_Equality(const A: TPoint; const B: string): Boolean; static;
end;
class function TStringListEx.&&op_In(const A: string; B: TStringListEx): Boolean;
begin
Result := B.IndexOf(A) >= 0;
end;
class function TPointHelper.&&op_Equality(const A: TPoint; const B: string): Boolean;
begin
Result := Format('%d,%d', [A.X, A.Y]) = B;
end;
var
sl: TStringListEx;
pt: TPoint;
ret: Boolean;
begin
sl := TStringListEx.Create;
sl.Add('AAA');
sl.Add('BBB');
ret := 'AAA' in sl;
Writeln(BoolToStr(ret, True)); // -> 'True'
pt := Point(123, 456);
ret := pt = '123,456';
Writeln(BoolToStr(ret, True)); // -> 'True'
Readln;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment