[fpc-devel] Operator overloading

Bart bartjunk64 at gmail.com
Sat Apr 13 19:48:41 CEST 2019


program op;

uses opovr;

procedure test;
var
  d: dword;
begin
  d:=(65 shl 24) or (66 shl 16) or (67 shl 8) or 68;
  if (d<>'ABCD') then
    writeln('d <> "ABCD"')
  else
    writeln('d = "ABCD"');
end;


begin
  test;
end.

unit opovr;

{$mode objfpc}
{$H+}

interface

operator = (L: dword; R: String) z: Boolean;


implementation

operator = (L: dword; R: String) z: Boolean;
begin
  z:=False;
end;

end.

After applying the patch from the bugtracker:

C:\Users\Bart\LazarusProjecten\bugs\Console\operator>fpc op.lpr -Mobjfpc -B
Free Pascal Compiler version 3.3.1 [2019/04/03] for i386
...
46 lines compiled, 0.1 sec, 35712 bytes code, 1316 bytes data

C:\Users\Bart\LazarusProjecten\bugs\Console\operator>op
d <> "ABCD" // operator overload from opovr.pp is used

C:\Users\Bart\LazarusProjecten\bugs\Console\operator>fpc op.lpr -Mmacpas
Free Pascal Compiler version 3.3.1 [2019/04/03] for i386
..
27 lines compiled, 0.2 sec, 79968 bytes code, 4244 bytes data

C:\Users\Bart\LazarusProjecten\bugs\Console\operator>op
d = "ABCD" // operator overload from opovr.pp is NOT used (you are
allowed to compare ordinal=string in MacPas mode by default)


C:\Users\Bart\LazarusProjecten\bugs\Console\operator>fpc op.lpr -Mmacpas -B
Free Pascal Compiler version 3.3.1 [2019/04/03] for i386
Copyright (c) 1993-2018 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling op.lpr
Compiling opovr.pp
opovr.pp(3,2) Error: Mode switch "OBJFPC" not allowed here
opovr.pp(8,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted

-Mmacpas disallows {$mode objfpc} in another unit?

Conclusion:
If you override the operator in a unit then that overload is NOT used
in a program that is built in MacPas mode (as long as the program uses
the ppu and does not try to rebuild the unit from source).

If that conclusion is correct, then the patch could be applied?
Or are there more caveats?

B.t.w. to my surprise it seems you cannot overload the = operator in
the opovr.pp unit if that unit uses {$mode Delphi}:
opovr.pp(8,1) Fatal: Syntax error, "IMPLEMENTATION" expected but
"identifier OPERATOR" found.

-- 
Bart



More information about the fpc-devel mailing list