[fpc-pascal] Generic operator overload problem

Ryan Joseph genericptr at gmail.com
Sat Nov 2 15:01:19 CET 2019


Is this a bug or did I do something wrong? The minus operator overload seems to be getting confused and I get a "Can't determine which overloaded function to call" error as a result.

{$mode objfpc}
{$modeswitch advancedrecords}

program generic_vector;
uses
  Math;

type
  generic TVec2<TScalar> = record
    x, y: TScalar;
    function Magnitude: TScalar;
    function Distance (vec: TVec2): TScalar;
    class operator - (left, right: TVec2): TVec2;  
    class operator - (left: TVec2; right: TScalar): TVec2; 
  end;
  TVec2f = specialize TVec2<Float>;
  TVec2i = specialize TVec2<Integer>;

function TVec2.Magnitude: TScalar;
begin
  result := (x ** 2) + (y ** 2);
end;

function TVec2.Distance (vec: TVec2): TScalar;
begin
  // ----->>> ERROR: Can't determine which overloaded function to call
  result := (self - vec).Magnitude;
end;

class operator TVec2.- (left, right: TVec2): TVec2;
begin
  result.x := left.x - right.x;
  result.y := left.y - right.y;
end;

class operator TVec2.- (left: TVec2; right: TScalar): TVec2; 
begin
  result.x := left.x - right;
  result.y := left.y - right;
end;

begin
end.

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list