Satisfying inheritence requirements

Jason House jason.james.house at gmail.com
Wed Oct 10 07:30:00 PDT 2007


Steven Schveighoffer Wrote:

> "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.

You're right, I should have had two different interfaces with A and B inheriting from different ones (or simply have B inherit from interface Foo).



> 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();} }


That's what I've been doing, but now that -profile is telling me bar is one of the largest time consumers in the program, I'm going back to trying to get aliasing to work (it was suggested on this mailing list in a past thread of the same title).

Using the terms from your example, bar is called 10x more than any other function.  The baz function is really simple (almost as simple as "return 1").  The total time per call is only 1 tick (microsecond).  Maybe it's just a fluke with the profiling, or maybe it really is significant.  I wanted to try the alias trick to see if it'd drop it in the overal profiler output.



More information about the Digitalmars-d-learn mailing list