www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - inheritance problem

reply bayo <bayo.fr gmail.com> writes:
Hello, i currently use DMD 1.013 for Windows and i cant compile this code


class C1 {
	public void add(int i, int j) {}
}

class C2 : C1 {
	public void add(int i) {}
}

void main() {
	C2 c = new C2();
	c.add(1, 2);
}

src\inheritance.d(16): function inheritance.C2.add (int) does not match 
parameter types (int,int)


I dont use D for a long time, but i dont remember having problem like this?

So anybody can help me:
*Is there a problem on my code?
*Is it a desired limitation?
*Can i use another version of DMD to remove this problem?

Thanks a lot for your help.

-bayo
Apr 26 2007
parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
bayo kirjoitti:
 Hello, i currently use DMD 1.013 for Windows and i cant compile this code
 
 
 class C1 {
     public void add(int i, int j) {}
 }
 
 class C2 : C1 {
     public void add(int i) {}
 }
 
 void main() {
     C2 c = new C2();
     c.add(1, 2);
 }
 
 src\inheritance.d(16): function inheritance.C2.add (int) does not match
 parameter types (int,int)
 
 
 I dont use D for a long time, but i dont remember having problem like this?
 
 So anybody can help me:
 *Is there a problem on my code?

Yes
 *Is it a desired limitation?

Yes, this is how inheritance works in D.
 *Can i use another version of DMD to remove this problem?

No.. ..but you can fix this by adding alias C1.add add; to your C2 class.
 
 Thanks a lot for your help.
 
 -bayo

Apr 26 2007
parent reply bayo <bayo.fr gmail.com> writes:
Jari-Matti Mäkelä a écrit :
 bayo kirjoitti:
 Hello, i currently use DMD 1.013 for Windows and i cant compile this code


 class C1 {
     public void add(int i, int j) {}
 }

 class C2 : C1 {
     public void add(int i) {}
 }

 void main() {
     C2 c = new C2();
     c.add(1, 2);
 }

 src\inheritance.d(16): function inheritance.C2.add (int) does not match
 parameter types (int,int)


 I dont use D for a long time, but i dont remember having problem like this?

 So anybody can help me:
 *Is there a problem on my code?

Yes
 *Is it a desired limitation?

Yes, this is how inheritance works in D.
 *Can i use another version of DMD to remove this problem?

No.. ..but you can fix this by adding alias C1.add add; to your C2 class.

Realy hard to me to understand this limitation if we can "patch" it with an alias, but thanks a lot for your fast help, it run well :)
 Thanks a lot for your help.

 -bayo


Apr 26 2007
parent reply Derek Parnell <derek psych.ward> writes:
On Thu, 26 Apr 2007 20:21:04 +0200, bayo wrote:

 Jari-Matti Mäkelä a écrit :
 bayo kirjoitti:
 Hello, i currently use DMD 1.013 for Windows and i cant compile this code


 class C1 {
     public void add(int i, int j) {}
 }

 class C2 : C1 {
     public void add(int i) {}
 }

 void main() {
     C2 c = new C2();
     c.add(1, 2);
 }

 src\inheritance.d(16): function inheritance.C2.add (int) does not match
 parameter types (int,int)


 I dont use D for a long time, but i dont remember having problem like this?

 So anybody can help me:
 *Is there a problem on my code?

Yes
 *Is it a desired limitation?

Yes, this is how inheritance works in D.
 *Can i use another version of DMD to remove this problem?

No.. ..but you can fix this by adding alias C1.add add; to your C2 class.

Realy hard to me to understand this limitation if we can "patch" it with an alias, but thanks a lot for your fast help, it run well :)

Well "limitation" may be a bit harsh. Inheritance is available but it is not as automatic as it is in some other languages. The need for the alias is because D's rules for finding matching functions is simple (some say simplistic). If it finds a matching name in the current context it doesn't try to look any further (e.g in the parent class). The 'alias' is the syntax needed to explicitly bring the parent's function's name into the current context. In your case above, because C2 contains the name 'add' your 'c.add(1,2)' stops looking further up the inheritance tree for a matching parameter signature. But by adding 'alias C1.add add', it includes all the 'add' functions in C1 into the namespace of C2. This is how Walter wants D to work so if you have suggested improvements, you have to convince him. (Good luck <G>) -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Apr 26 2007
next sibling parent Aarti_pl <aarti interia.pl> writes:
Derek Parnell napisał(a):
 On Thu, 26 Apr 2007 20:21:04 +0200, bayo wrote:
 
 Jari-Matti Mäkelä a écrit :
 bayo kirjoitti:
 Hello, i currently use DMD 1.013 for Windows and i cant compile this code




<snip>

an alias, but thanks a lot for your fast help, it run well :)

Well "limitation" may be a bit harsh. Inheritance is available but it is not as automatic as it is in some other languages. The need for the alias is because D's rules for finding matching functions is simple (some say simplistic). If it finds a matching name in the current context it doesn't try to look any further (e.g in the parent class). The 'alias' is the syntax needed to explicitly bring the parent's function's name into the current context. In your case above, because C2 contains the name 'add' your 'c.add(1,2)' stops looking further up the inheritance tree for a matching parameter signature. But by adding 'alias C1.add add', it includes all the 'add' functions in C1 into the namespace of C2. This is how Walter wants D to work so if you have suggested improvements, you have to convince him. (Good luck <G>)

It seems that this behavior is similar to way mixins work. I reported it as enhancement as current way has serious limitations. http://d.puremagic.com/issues/show_bug.cgi?id=1182 Regards Marcin Kuszczak (aarti_pl)
Apr 27 2007
prev sibling parent kenny <funisher gmail.com> writes:
 Well "limitation" may be a bit harsh. Inheritance is available but it is
 not as automatic as it is in some other languages.
 
 The need for the alias is because D's rules for finding matching functions
 is simple (some say simplistic). If it finds a matching name in the current
 context it doesn't try to look any further (e.g in the parent class). The
 'alias' is the syntax needed to explicitly bring the parent's function's
 name into the current context.
 
 In your case above, because C2 contains the name 'add' your 'c.add(1,2)'
 stops looking further up the inheritance tree for a matching parameter
 signature. But by adding 'alias C1.add add', it includes all the 'add'
 functions in C1 into the namespace of C2.
 
 This is how Walter wants D to work so if you have suggested improvements,
 you have to convince him. (Good luck <G>)
 

Perhaps the error can instead say something more useful like suggesting to alias instead.
Apr 30 2007