Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2019-04

Operator overloading and {$mode delphi}

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

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?

Bart

Reply by email

Sven Barth
to Bart
Bart wrote 51 lines
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

Reply by email

Ben Grasset
to Sven Barth

On Sat, Apr 13, 2019 at 6:04 PM Sven Barth via fpc-devel <

wrote: Maybe we should mention that in the documentation...

AFAIK it's never been possible to use FPC-style operator overloading in mode Delphi... is it supposed to be allowed?

For example:

unit Unit1;
{$mode delphi}
interface
uses
  Classes, SysUtils;
type
  TRec = record
  end;
operator Implicit(constref ARec: TRec): String;
implementation
operator Implicit(constref ARec: TRec): String;
begin
end;
end.
That program gives: "unit1.pas(14,10) Fatal: Syntax error, "=" expected but
"identifier IMPLICIT" found"

Why it is specifically expecting "=", I have no idea. In general though I don't recall ever having successfully used non-"class operator" operators in {$mode Delphi}.

WRT the docs, there's a lot of things they don't mention. They entirely ignored the fact that records and objects (not just classes) can be enumerators until a few days ago.

Of course, nobody's gonna know the docs have been updated for a long long time, because they only get built every couple of years or so at best... (Unless you do it yourself, which is not even *that* hard, but still way harder than it *should* be in a manner that is rather far removed from FPC's usual cross-platform nature for pretty dubious results: i.e. the docs are pretty much *just ok*, not super great or anything.)

Reply by email

Michael Van Canneyt
to Sven Barth: In mode Delphi you need to use the names Delphi uses for the operators:…
Sven Barth wrote 10 lines
On 4/13/19 11:28 PM, Bart wrote: > Hi, > > 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...

I added a table with the delphi operator names, mentioned that they can be used in operator definitions, but that they must be used in Delphi mode.

Michael.

Reply by email

Bart
to Michael Van Canneyt
AM Michael Van Canneyt wrote
I added a table with the delphi operator names, mentioned that they can be used in operator definitions, but that they must be used in Delphi mode.

If I understand the delphi documentation correctly the you can only overload operaters using class methods for records or classes? So, you cannot overload an operator to add an ordinal to a string (which we can in fpc).

Bart

Reply by email

Sven Barth
to Bart
Bart wrote 9 lines
On Sun, Apr 14, 2019 at 8:26 AM Michael Van Canneyt wrote: > I added a table with the delphi operator names, mentioned that they can be > used in operator definitions, but that they must be used in Delphi mode. If I understand the delphi documentation correctly the you can only overload operaters using class methods for records or classes? So, you cannot overload an operator to add an ordinal to a string (which we can in fpc).

Correct. Delphi does not support global overloads. I know that it's a bit inconsistent that we allow some FPC features in mode Delphi and some not, but the main idea of mode Delphi is to allow the compilation of Delphi code.

Regards,
Sven

Reply by email