mixin on identifier

David Nadlinger see at klickverbot.at
Mon Nov 21 20:57:39 PST 2011


Turns out to be surprisingly tricky… A possible solution is:

mixin template StateOne() {
   int value;
}

mixin template StateTwo() {
   float data;
}

mixin template MixinAll(T...) {
   static if (T.length > 0) {
     alias T[0] A;
     mixin A;
     mixin MixinAll!(T[1 .. $]);
   }
}

class StateSet(T...){
   mixin MixinAll!T;
}

void main() {
   auto s = new StateSet!(StateOne, StateTwo)();
}

Hope this helps,
David


On 11/22/11 1:02 AM, Johannes Totz wrote:
> On 21/11/2011 23:39, David Nadlinger wrote:
>> Make T an alias parameter:
>> class StateSet(alias T) { … }
>>
>> David
>
> Thanks!
>
> I was trying to get to something like this:
>
> mixin template StateOne()
> {
> int value;
> }
>
> mixin template StateTwo()
> {
> float data;
> }
>
> class StateSet(alias T ...)
> {
> mixin T;
> }
>
>
> int main(string[] argv)
> {
> StateSet!(StateOne, StateTwo) s = new StateSet!(StateOne, StateTwo)();
>
> return 0;
> }
>
>
> With the docs I got to:
>
> template StateSet(alias T, S ...)
> {
> class StateSet
> {
> mixin T;
> }
> }
>
> But I have no clue how to expand S into mixin statements.
>
>
>
>> On 11/22/11 12:38 AM, Johannes Totz wrote:
>>>
>>> mixin template StateOne()
>>> {
>>> int value;
>>> }
>>>
>>> class StateSet(T)
>>> {
>>> mixin T;
>>> }
>>>
>>> int main(string[] argv)
>>> {
>>> StateSet!StateOne s = new StateSet!StateOne();
>>>
>>> return 0;
>>> }
>>
>



More information about the Digitalmars-d-learn mailing list