Satisfying inheritence requirements
Steven Schveighoffer
schveiguy at yahoo.com
Wed Oct 10 06:58:43 PDT 2007
"Jason House" wrote
> When inheriting from a super class and an interface, I can't seem to get
> aliasing to work (to satisfy the interface requirements). Below is a
> simple session demonstrating the problem. I've tested with dmd 1.018,
> 1.020, and 2.003.
>
> $ cat test.d
> interface Foo{ int bar(); }
> class A : Foo{ int bar(){return 1;} }
> class B : A, Foo{ alias A.bar bar; }
> void main(){}
>
> $ dmd test.d
> test.d(3): class test.B interface function Foo.bar is not implemented
I'm thinking you have either an incorrectly written example, or you are
misunderstanding inheritance. To make this compile, just have B inherit
from A. Because A implements Foo, B also implements Foo. As far as I know,
an alias cannot satisfy interface requirements, but you don't need it for
this.
e.g.:
> interface Foo{ int bar(); }
> class A : Foo{ int bar(){return 1;} }
> class B : A { }
> void main(){}
Should compile, and B.bar() should return 1.
Now, to give an example where an alias is needed to satisfy interface
requirements:
> interface Foo{ int bar(); }
> class A { int baz(){return 1;} }
> class B : A, Foo{ alias A.baz bar;}
> void main(){}
I don't think this will work, and the only way around it is:
> class B : A, Foo{ int bar(){return baz();} }
-Steve
More information about the Digitalmars-d-learn
mailing list