Why oh why can't I manipulate the order in which overloaded methods get selected...

Why oh why can't I manipulate the order in which overloaded methods get selected...

SomeClass.....
class function Compare(const Left, Right: Integer): Integer; overload; static; inline;
class function Compare(const Left, Right: T): integer; overload; static; inline;

The generic one always has preference :(

Changing it to:

class function Compare(const Left, Right: TRecWithCustomOperators): integer; overload; static; inline;

Fixes the issue, but then I get ambiguous overloaded calls on enums and the like.

Grr.

Comments

  1. Because the generic is more specific?

    type
      TRec = record
        class procedure Func(const x: integer); overload; static;
        class procedure Func(const x: T); overload; static;
      end;
    { TRec }
    class procedure TRec.Func(const x: integer);
    begin
      WriteLn('plain');
    end;
    class procedure TRec.Func(const x: T);
    begin
      WriteLn('generic');
    end;
    begin
      TRec.Func('foo'); // outputs generic
      TRec.Func(42); // outputs plain
      ReadLn;
    end.

    Perhaps explain a bit more about what you're trying to do here?

    ReplyDelete
  2. You can simply give methods different names.

    ReplyDelete
  3. David Heffernan That's what I settled on.

    ReplyDelete

Post a Comment