Strange Mixin issue

Ali Çehreli acehreli at yahoo.com
Wed Mar 5 14:35:45 PST 2014


On 03/05/2014 01:30 PM, Frustrated wrote:
> I am trying to remove the unnecessary passing of the type of class to a
> template but can't seem to get it to work:
>
> see
>
>
>
> The code is the
>
>
>      mixin(AbstractToInterface!(WindowsGui, iButton, WindowsButton,
> iBorder, WindowsBorder));
>
> which I want to not have to specify WindowsGui.
>
> I've tried wrapping AbstractToInterface in a mixin template and use that
> and typeof(this) but then the mixin of the mixin does not get mixed in
> to the class
>
> e.g.,
>
> mixin template AbstractToInterface2(T...)
> {
>      mixin(AbstractToInterface!(typeof(this), T));
> }
>
> Then use mixin AbstractTointerface2!(iButton, WindowsButton, iBorder,
> WindowsBorder).
>
> It's probably something stupid but I can't seem to get it to work(have a
> very similar case in some other code and it works fine... the only
> difference is I'm not using nested mixins.
>

The following works for me maybe because the templates are not mixin 
templates. What's a mixin template again?

import std.typetuple;

template fooImpl(This, T...)
{
     static assert(is (This == S));        // <-- IT WORKED!
     static assert(is (T == TypeTuple!(int, double)));
}

template foo(T...)
{
     alias foo = fooImpl!(typeof(this), T);
}

struct S
{
     mixin foo!(int, double);
}

void main()
{
     auto s = S();
}

Even better:

import std.typetuple;

template fooImpl(T...)
{
     static assert(is (T[0] == S));    // <-- COOL!
     static assert(is (T[1] == int));
     static assert(is (T[2] == double));
}

template foo(T...)
{
     alias foo = fooImpl!(TypeTuple!(typeof(this), T));
}

struct S
{
     mixin foo!(int, double);
}

void main()
{
     auto s = S();
}

Ali



More information about the Digitalmars-d-learn mailing list