Thread overview
[Bug] Compiler cannot find the path to a method
Apr 10, 2004
Eric
Apr 10, 2004
larry cowan
Apr 14, 2004
Eric
Apr 14, 2004
Ben Hinkle
April 10, 2004
Hi everybody,

The compiler throw me the following error :

function buggyfunction overloads void(A arg1,A arg2) and void(C arg1,A arg2) both match argument list for buggyfunction

when trying to compile the following code :

class A {
  void buggyfunction(A arg1, A arg2){}
  void buggyfunction(C arg1, A arg2){}
}

class B : A {
  void buggyfunction(A arg1, A arg2){}
  void buggyfunction(C arg1, A arg2){}
}

class C : A {
  void buggyfunction(A arg1, A arg2){}
  void buggyfunction(C arg1, A arg2){}
}

void main(){
  B objb = new B();
  B b    = new B();
  C c    = new C();

  //This line is ok
  objb.buggyfunction(b,b);

  //Throw error on this line
  objb.buggyfunction(c,b);
}

I think the compiler should be able to determine that it need to call 'void buggyfunction(C arg1, A arg2)' of class B.

I am new to D, so if it's not a bug let me know how to do it properly.
I am using dmd 0.81 on Linux.

Thanks
Eric
April 10, 2004
In article <c57gh5$194v$1@digitaldaemon.com>, Eric says...
>
>Hi everybody,
>
>The compiler throw me the following error :
>
>function buggyfunction overloads void(A arg1,A arg2) and void(C arg1,A arg2) both match argument list for buggyfunction
>
>when trying to compile the following code :
>
>class A {
>   void buggyfunction(A arg1, A arg2){}
>   void buggyfunction(C arg1, A arg2){}
>}
>
>class B : A {
>   void buggyfunction(A arg1, A arg2){}
>   void buggyfunction(C arg1, A arg2){}
>}
>
>class C : A {
>   void buggyfunction(A arg1, A arg2){}
>   void buggyfunction(C arg1, A arg2){}
>}
>
>void main(){
>   B objb = new B();
>   B b    = new B();
>   C c    = new C();
>
>   //This line is ok
>   objb.buggyfunction(b,b);

In the above line, trying the types given (B,B) does not match anything, so
trying the possible implicit casts (A,B) (B,A) (A,A) it finds only a match for
(A,A) so the compiler accepts that.
>
>   //Throw error on this line
>   objb.buggyfunction(c,b);

In the above line, again there is no (C,B) match, but (C,A) (A,B) (A,A) provides
2 possible matches - for (C,A) or (A,A) - the compiler isn't saying it can't
find a match, it is saying it found too many possible answers once it had to
start looking:
>}
>
>I think the compiler should be able to determine that it need to call 'void buggyfunction(C arg1, A arg2)' of class B.
>
>I am new to D, so if it's not a bug let me know how to do it properly. I am using dmd 0.81 on Linux.
>
>Thanks
>Eric

Try explicit casting:
(c,cast(A)b)         or
(cast(A)c,cast(A)b)  or
(cast(A)c,b)         to say how you want it handled.


April 14, 2004
Thanks for the answer Larry,

Okay this is not a bug then, it's a feature request ;)

It works fine if I explicitly cast the object, I just don't see the need to do it. I think the compiler should be smart enough to decide the best method to call, ie implicit cast into the most recent class in the class hierarchy.

Cast makes program harder to read and I would like to avoid using a lot of cast for something that could be done implicitly. It works implicitly for other modern language like C#. I have not test in Java, but I think it would do it implicitly too. (Anyone know?) So why not do it for D if it can make the language, in my opinion, more intuitive and easier on the eye.

Have a nice day!
Eric

larry cowan wrote:
> In article <c57gh5$194v$1@digitaldaemon.com>, Eric says...
> 
>>Hi everybody,
>>
>>The compiler throw me the following error :
>>
>>function buggyfunction overloads void(A arg1,A arg2) and void(C arg1,A arg2) both match argument list for buggyfunction
>>
>>when trying to compile the following code :
>>
>>class A {
>>  void buggyfunction(A arg1, A arg2){}
>>  void buggyfunction(C arg1, A arg2){}
>>}
>>
>>class B : A {
>>  void buggyfunction(A arg1, A arg2){}
>>  void buggyfunction(C arg1, A arg2){}
>>}
>>
>>class C : A {
>>  void buggyfunction(A arg1, A arg2){}
>>  void buggyfunction(C arg1, A arg2){}
>>}
>>
>>void main(){
>>  B objb = new B();
>>  B b    = new B();
>>  C c    = new C();
>>
>>  //This line is ok
>>  objb.buggyfunction(b,b);
> 
> 
> In the above line, trying the types given (B,B) does not match anything, so
> trying the possible implicit casts (A,B) (B,A) (A,A) it finds only a match for
> (A,A) so the compiler accepts that.
> 
>>  //Throw error on this line
>>  objb.buggyfunction(c,b);
> 
> 
> In the above line, again there is no (C,B) match, but (C,A) (A,B) (A,A) provides
> 2 possible matches - for (C,A) or (A,A) - the compiler isn't saying it can't
> find a match, it is saying it found too many possible answers once it had to
> start looking:
> 
>>}
>>
>>I think the compiler should be able to determine that it need to call 'void buggyfunction(C arg1, A arg2)' of class B.
>>
>>I am new to D, so if it's not a bug let me know how to do it properly.
>>I am using dmd 0.81 on Linux.
>>
>>Thanks
>>Eric
> 
> 
> Try explicit casting: (c,cast(A)b)         or
> (cast(A)c,cast(A)b)  or
> (cast(A)c,b)         to say how you want it handled.
> 
> 
April 14, 2004
On Tue, 13 Apr 2004 22:12:03 -0400, Eric <olace99@hotmail.com> wrote:

>Thanks for the answer Larry,
>
>Okay this is not a bug then, it's a feature request ;)

Walter's rationale for the current behavior is in the section about overloading in http://www.digitalmars.com/d/function.html

-Ben