Sign in Join

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

Mailing lists / fpc-pascal / 2022-06

Operator not overloaded

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Thomas Kurz

Hello,

I have tried to create an example as minimalistic as possible. I have three files:

*** file: pathfinding.pas ***

unit PathFinding;
{$mode objfpc}
interface
type generic TAStar<T> = class (TObject)
  public function FindPath (const AStart: T; const ADestination: T): Boolean; virtual;
end;
implementation
function TAStar.FindPath (const AStart: T; const ADestination: T): Boolean;
  var current: T;
  begin
    current:=Default(T);
    if (current = ADestination) then Exit (false);
  end;
end.

*** file: testtypes.pas ***

unit testTypes;
{$mode objfpc}
interface
type TTileSegment = record
  A: NativeUInt;
  B: NativeUInt;
end;
operator = (ALeft: TTileSegment; ARight: TTileSegment): Boolean;
operator < (ALeft: TTileSegment; ARight: TTileSegment): Boolean;
operator > (ALeft: TTileSegment; ARight: TTileSegment): Boolean;
implementation
operator = (ALeft: TTileSegment; ARight: TTileSegment): Boolean;
  begin
    Exit (True);
  end;
operator < (ALeft: TTileSegment; ARight: TTileSegment): Boolean;
  begin
    Exit (True);
  end;
operator > (ALeft: TTileSegment; ARight: TTileSegment): Boolean;
  begin
    Exit (True);
  end;
end.

*** file: project1.lpr ***

program project1;
{$mode objfpc}
uses testtypes, PathFinding;
var x: specialize tastar<ttilesegment>;
begin
  x := specialize tastar<ttilesegment>.create;
end.

**************

When compiling, I get this error:

pathfinding.pas(17,17) Error: Operator is not overloaded: "TTileSegment" = "TTileSegment"

Which I don't understand -- because the "=" operator is defined in tesstypes.pas.

Am I doing something wrong or is this behavior a bug?

Kind regards,
Thomas

Sven Barth
to Thomas Kurz

Thomas Kurz via fpc-pascal schrieb am
Do., 23. Juni 2022, 19:45:

Show quoted text 10 lines
When compiling, I get this error: pathfinding.pas(17,17) Error: Operator is not overloaded: "TTileSegment" = "TTileSegment" Which I don't understand -- because the "=" operator is defined in tesstypes.pas. Am I doing something wrong or is this behavior a bug?

Global operator overloads need to be available at the time the generic is *declared*, not *specialized*. That is one of the reasons why advanced records were introduced.

So you need to declare your operators as part of your record.

Regards,
Sven

Thomas Kurz
to Sven Barth

Thank you very much, declaring the operator within the record does indeed solve the issue.

Just to be sure in case I might need it some day: Does this mean, if I need e.g. a comparison operator for a specialized TPair, I would declare a record class helper and define the operator there?

Earlier message 28 lines
----- Original Message ----- From: Sven Barth via fpc-pascal To: FPC-Pascal users discussions Sent: Friday, June 24, 2022, 10:43:55 Subject: [fpc-pascal] Operator not overloaded Thomas Kurz via fpc-pascal schrieb am Do., 23. Juni 2022, 19:45: > When compiling, I get this error: > pathfinding.pas(17,17) Error: Operator is not overloaded: "TTileSegment" = > "TTileSegment" > Which I don't understand -- because the "=" operator is defined in > tesstypes.pas. > Am I doing something wrong or is this behavior a bug? Global operator overloads need to be available at the time the generic is *declared*, not *specialized*. That is one of the reasons why advanced records were introduced. So you need to declare your operators as part of your record. Regards, Sven
Sven Barth
to Thomas Kurz

Thomas Kurz via fpc-pascal schrieb am
Fr., 24. Juni 2022, 15:33:

Show quoted text 7 lines
Thank you very much, declaring the operator within the record does indeed solve the issue. Just to be sure in case I might need it some day: Does this mean, if I need e.g. a comparison operator for a specialized TPair, I would declare a record class helper and define the operator there?

No, because for helpers the same restrictions apply as they do for global operators.

Regards,
Sven