mixin template

Frustrated c1514843 at drdrb.com
Fri Jan 31 04:52:07 PST 2014


On Friday, 31 January 2014 at 06:26:16 UTC, Dan Killebrew wrote:
> On Friday, 31 January 2014 at 06:24:27 UTC, Dan Killebrew wrote:
>>> mixin template Foo(alias a){ alias Foo=a; }
>>> pragma(msg, Foo!2); // error
>>>
>>> template Bar(alias a){ alias Bar=a; }
>>> pragma(msg, Bar!2); // ok
>>
>> As far as I can tell, 'mixin template' does nothing new;
>
> (besides fail to compile in Timon's reply). I should have said 
> "it does nothing helpful."

Using a mixin template forces it to be used as a mixin which,
sometimes you need.

It does something new because it is different.

If you drop the mixin and use a template then it is a different
construct than than a mixin template.

e.g.,

template X()
{
      static this() { .... }
}

mixin template Y()
{
      static this() { .... }
}

class A
{
     mixin X;
}

class B
{
     mixin Y;
}

class C
{
     X;   // ok because X is a normal template
}

class D
{
     Y;   // error, Y has to be mixed in since it is a mixin
template
}


A and B are the same. C compiles fine. D fails because we are
using Y like it was a normal template function not it is not.

We might want that because a mixin template and a template have
different meanings. One mixes in the code on site and one
doesn't. If you write a mixin template and it would fail if it
were used as a template then you should make sure to use the
mixin in front of it.

One could argue that they should have been disjoint, i.e., a
normal template should not be able to be mixed in and a mixin
template must be mixined. This was not done.

If you write a mixin template and it can't or shouldn't be used
as a normal template, then use the 'mixin template'. This
prevents it from being used as a normal template.

If you write a normal template and do not want it to be mixed in,
you are out of luck. Not sure if their was a good reason for this
or not. (probably ok to use and in some cases very useful to have
so it was made optional)


More information about the Digitalmars-d-learn mailing list