digitalmars.D.learn - Are there any mixin tutorials?
- "ixid" <nuaccount gmail.com> Apr 23 2012
- David <d dav1d.de> Apr 23 2012
- bearophile <bearophileHUGS lycos.com> Apr 23 2012
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
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
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









David <d dav1d.de> 