Strange Mixin issue

Ali Çehreli acehreli at yahoo.com
Wed Mar 5 15:04:05 PST 2014


On 03/05/2014 02:37 PM, Frustrated wrote:

 >> 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
 >
 > this is not quite the same.

I think you tried to give a link in your original code but it is 
missing. Still, it is very difficult for me to follow code unless it is 
really simple. :)

 > You have static asserts.

Just to prove that I've managed to inject typeof(this) as the first 
template parameter.

 > I'm trying to avoid
 > mixing in ctfe code that might interfer with the class. (I have a temp
 > function Do which if I mixin as a normal template will mixin Do into the
 > class make Do a member of that class)

The following is what I understand so far. :) The last line of main() 
proves that S gained a function Do() that takes (int, double) parameters.

import std.typetuple;

template DoImpl(T...)
{
     auto Do(T[1] p1, T[2] p2)
     {
         return p1 + p2;
     }
}

template Do(T...)
{
     mixin DoImpl!(TypeTuple!(typeof(this), T));
}

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

void main()
{
     auto s = S();
     assert(s.Do(1, 2.5) == 3.5);
}

Ali



More information about the Digitalmars-d-learn mailing list