Function definition in multiple modules

simendsjo simendsjo at gmail.com
Thu Mar 29 04:05:07 PDT 2012


On Thu, 29 Mar 2012 12:58:56 +0200, Martin Drasar <drasar at ics.muni.cz>  
wrote:

> On 29.3.2012 12:02, simendsjo wrote:
>> Your looking for partial classes? D doesn't have this as far as I know.
>>
>> "alias this" should work for more than one value in the future, and then
>> (I think) you should be able to do something like this:
>>
>> class XIB : IB {}
>> class XIA : IA {}
>> class X : IA, IB {
>>   XIA xia;
>>   XIB xib;
>>   alias xia this;
>>   alias xib this;
>> }
>
> I was thinking about similar idea. But instead of alias this I intended
> to write something like this:
>
> class X : IA, IB {
>   XIA xia;
>
>   void iamethod()
>   {
>     xia.iamethod();
>   }
>   ...
> }

You can also use opDispatch and __traits(getMember, ..). Don't know if it  
works when implementing interfaces though.

> It is not very pretty though...
>
>> But you can also solve it with mixin templates:
>> mixin template XIA {
>>   // all IA methods
>> }
>> mixin template XIB {
>>   // all IB methods
>> }
>>
>> class X : IA, IB {
>>   mixin XIA!();
>>   mixin XIB!();
>> }
>
> This would make the code separation easy to manage, but I am concerned
> about debugging. How is e.g. gdb able to debug mixins? I think I have
> read somewhere in the D or D-learn, that the dmd can produce the file
> with mixed-in code for the debugger, but I am not sure how to find it.
>
> Martin

It's not string mixins:
mixin template XIA() {
   void a() { ... } // regular function
}
class X : IA {
   mixin XIA!()
}

XIA is injected into X, so X now looks like
class X : IA {
   void a() { ... }
}


More information about the Digitalmars-d-learn mailing list