On Thursday, 30 January 2014 at 17:12:51 UTC, Etienne wrote:
On 2014-01-30 10:28 AM, Timon Gehr wrote:
import std.typetuple, std.stdio;
void main(){
    foreach(i; TypeTuple!(1,2,3)){
        mixin("int num"~i.stringof~";");
    }
    num1=1;
    num2=2;
    num3=3;
    writeln(num1,num2,num3);
}



This written as a static foreach or declarative foreach, would be awesome if it exposed the scope. For now the only solution is to build a string and mixin the string like this

string fct(R)(){
        string str;
        foreach (range ; R)
                str ~= "int num " ~ range.stringof ~ ";";

        return str;
}

mixin(fct!(TypeTuple!(1,2,3));
writeln(num1,num2, num3);

Looks a little less convenient than

static foreach ( i; 0..3 )
        mixin("int num" ~ i.stringof ~ ";");
writeln(num1,num2, num3);

It works now:
    mixin(format("%(int num%s; %);", [1,2,3])); //<----
    writeln(num1,num2,num3);

Reply via email to