[fpc-devel] Operator overloading and {$mode delphi}

Sven Barth pascaldragon at googlemail.com
Sun Apr 14 00:03:37 CEST 2019


On 4/13/19 11:28 PM, Bart wrote:
> Hi,
> 
> This example is taken from the documentation:
> https://www.freepascal.org/docs-html/ref/refse103.html
> 
> {$mode delphi}  //compiles in {$mode objfpc} or {$mode fpc}
> {$h+}
> 
> Type
>   TMyRec = record a,b : integer; end;
> 
> operator = (x,y : TMyRec) r : string;
> 
> begin
>   if (x.a=y.a) and (x.b=y.b) then
>     R:='equal'
>   else
>     R:='differ';
> end;
> 
> var
>   x,y : TMyRec;
> 
> begin
>   x.a:=1;
>   y.a:=1;
>   Writeln(x=y);
>   x.a:=2;
>   y.a:=3;
>   Writeln(x=y);
> end.
> 
> This will compile in {$mode objfpc}, but not in {$mode delphi}:
> C:\Users\Bart\LazarusProjecten\ConsoleProjecten>fpc test.pas
> Free Pascal Compiler version 3.3.1 [2019/04/03] for i386
> ...
> Compiling test.pas
> test.pas(7,17) Fatal: Syntax error, ")" expected but ":" found
> Fatal: Compilation aborted
> (Same for 3.0.4)
> 
> The documentations never states that operator overloading is
> impossible in delphi mode.
> It even explicitely says
> (https://www.freepascal.org/docs-html/ref/refse100.html#x209-23100015.2):
> "Remark: When compiling in Delphi mode or Objfpc mode, the result
> identifier may be dropped. The result can then be accessed through the
> standard Result symbol."
> 
> What am I missing?
> 

In mode Delphi you need to use the names Delphi uses for the operators:
http://docwiki.embarcadero.com/RADStudio/Rio/en/Operator_Overloading_(Delphi)

Maybe we should mention that in the documentation...

Regards,
Sven



More information about the fpc-devel mailing list