www.digitalmars.com         C & C++   DMDScript  

D - [Bug] Compiler cannot find the path to a method

reply Eric <olace99 hotmail.com> writes:
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
Apr 09 2004
parent reply larry cowan <larry_member pathlink.com> writes:
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.
Apr 09 2004
parent reply Eric <olace99 hotmail.com> writes:
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 

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.
Apr 13 2004
parent Ben Hinkle <bhinkle4 juno.com> writes:
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
Apr 14 2004