structs, classes, interfaces - Part III, Solution

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Sat Sep 1 10:36:49 PDT 2007


Daniel Keep wrote:

> 
> 
> Jari-Matti Mäkelä wrote:
>> ...
>> 
>> interface IA { void foo(); }
>> interface IB { int bar(); }
>> 
>> struct A : IA { void foo() { writefln("hello"); }
>> struct B : IB { int bar() { return 42; } }
>> 
>> struct C : IA, IB {
>>   mixin A;
>>   mixin B;
>> }
> 
> I believe this will be possible using:
> 
> struct C : IA, IB {
>   private A a;
>   private B b;
>   alias a this;
>   alias b this;
> }
> 

I don't doubt it isn't possible, but it needs to be implemented first too :)
One problem I see in the new aliasing syntax is that it introduces two
unnecessary symbols C.a and C.b as an side effect. (but it could allow you
to choose between a.foo and b.foo if foo happens to conflict, though)

Another way to implement this could be to allow "template inheritance". In
that case only non-virtual inheritance model needs to be implemented, no
new mixins or aliases:

>> template A : IA { void foo() { writefln("hello"); }
>> template B : IB { int bar() { return 42; } }
>> 
>> struct C : IA, IB {
>>   mixin A!(); // or maybe even mixin A; when there are no arguments
>>   mixin B!();
>> }



More information about the Digitalmars-d mailing list