Generic operator overload problem

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Generic operator overload problem

Free Pascal - General mailing list
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generic operator overload problem

Free Pascal - General mailing list
Am 02.11.2019 um 15:01 schrieb Ryan Joseph via fpc-pascal:

> 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.
This indeed appears to be a bug, but fixing it leads to some compilation
problems in Generics.Collections that need to be solved as well. So
please report this as a bug so that I don't forget it.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Generic operator overload problem

Free Pascal - General mailing list


> On Nov 8, 2019, at 5:32 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> This indeed appears to be a bug, but fixing it leads to some compilation problems in Generics.Collections that need to be solved as well. So please report this as a bug so that I don't forget it.

Great, thanks.

https://bugs.freepascal.org/view.php?id=36285

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal