Are there any mixin tutorials?

David d at dav1d.de
Mon Apr 23 07:59:12 PDT 2012


Am 23.04.2012 16:51, schrieb ixid:
> Or any other appropriate methods to achieve things like:
>
> Loop unrolling
> foreach(i;0..12)
> //stuff involving the value
>

You can use this tuple-range, which is Compile-Time:

template TupleRange(int from, int to) {
     alias TupleRangeImpl!(to-1, from) TupleRange;
}

private template TupleRangeImpl(int to, int now) {
     static if(now == to) {
         alias TypeTuple!(now) TupleRangeImpl;
     } else {
         alias TypeTuple!(now, TupleRangeImpl!(to, now+1)) TupleRangeImpl;
     }
}

foreach(i; TupleRange!(0, 12)) { ... }


> And making multiple functions with different values:
> func1(int a) {//stuff}
> func2(int a) {//stuff}
> func3(int a) {//stuff}
>
> where func 1 to 3 are all generated at compile time using the
> same template and different values in their function bodies. I'm
> trying to make sense of the docs for this but it's not very clear.

Doing this involves atleast one String-Mixin:

mixin template Funcs() {
     string generate_functions() {
         return "string which contains all functions: func1(int a) { …} 
func2(int a) { …} func3(int a) { …}";
     }

     mixin(generate_functions());
}

mixin!Funcs()


^ Something like this should work.


More information about the Digitalmars-d-learn mailing list