pio pio
pio pio

Reputation: 796

Interfaces and overload directive

The following code throws me the compiler error

E2252 Method 'MyFunction' with identical parameters already exists

program Project3;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  aMyInterface = interface
    function MyFunction(const aSort: Integer; var aEndPoint: Integer): Integer; overload;
    function MyFunction(const aSort, aStartingPoint: Integer): Integer; overload;
  end;

  aMyClass = class(TInterfacedObject, aMyInterface)
    function MyFunction(const aSort: Integer; var aEndPoint: Integer): Integer; overload;
    function MyFunction(const aSort, aStartingPoint: Integer): Integer; overload;
  end;

{ aMyClass }

function aMyClass.MyFunction(const aSort: Integer; var aEndPoint: Integer): Integer;
begin
  Result := 1;
end;

function aMyClass.MyFunction(const aSort, aStartingPoint: Integer): Integer;
begin
  Result := 1;
end;

begin
end.

I understand there are two variables Integer type for each instance of the function but in one function both variables are both const and in the other function one variable one is const and the other one is var.

Why isn't that sufficient not to be considered identical parameters?

Upvotes: 1

Views: 274

Answers (1)

David Heffernan
David Heffernan

Reputation: 613222

Because the overload resolution decision is taken by considering the calling code and not the declaration.

Suppose you call the function like this:

MyFunction(int1, int2);

Which one do you expect to be called? The const overload or the var overload? The compiler has no means to take that decision. Hence this is deemed ambiguous.

Upvotes: 6

Related Questions