digitalmars.D.learn - [D2] Variadic template alias paramaters?
- Adam D. Ruppe (46/46) Oct 27 2009 I'm trying to write a little function that fills in some provided variab...
I'm trying to write a little function that fills in some provided variables by prompting the user for each one by name. For a set number of variables, this is easy: ======= import std.stdio; import std.conv; template GetData(alias A, alias B, alias C) { void GetData(string message) { writefln("%s\n", message); writef("%s: ", A.stringof); A = to!(typeof(A))(readln()); writef("%s: ", B.stringof); B = to!(typeof(B))(readln()); writef("%s: ", C.stringof); C = to!(typeof(C))(readln()); } } void main() { string first, last, email; GetData!(first, last, email)("Enter your contact info"); writefln("\nGot:"); writef("first: %s", first); writef("last: %s", last); writef("email: %s", email); } ======== Prints the variable names and fills them in, perfectly. But I'd like to make it more generic as to the number of parameters passed in - you can see that A, B, and C are all treated the same way. I tried making it GetData(alias A...), but that wouldn't compile. Making it GetData(A...) compiles, but does the wrong thing: ==== template GetData(A...) { void GetData(string message) { writefln("%s\n", message); foreach(a; A) { writef("%s: ", a.stringof); a = to!(typeof(a))(readln()); } } } ===== This prints: Enter your contact info: a: a: a: And doesn't fill in the variables. I also tried GetData(alias A[]), but that doesn't compile either. Is there any way I can get multiple alias parameters to the template, that I can preferably foreach over?
Oct 27 2009