www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are there any mixin tutorials?

reply "ixid" <nuaccount gmail.com> writes:
Or any other appropriate methods to achieve things like:

Loop unrolling
foreach(i;0..12)
        //stuff involving the value

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.
Apr 23 2012
next sibling parent David <d dav1d.de> writes:
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.
Apr 23 2012
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
ixid:

 Or any other appropriate methods to achieve things like:
 
 Loop unrolling
 foreach(i;0..12)
         //stuff involving the value

See Iota!() I've written here: http://d.puremagic.com/issues/show_bug.cgi?id=4085 Bye, bearophile
Apr 23 2012