Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
Created May 27, 2013 10:18
Show Gist options
  • Save JakobOvrum/5656352 to your computer and use it in GitHub Desktop.
Save JakobOvrum/5656352 to your computer and use it in GitHub Desktop.
DeclareVars - declare variables in-place given a type and list of identifiers
// Could use `alias front` instead to bind to single characters as well
mixin template DeclareVars(T, string front, names...)
{
mixin("T " ~ front ~ ";");
static if(names.length > 0)
mixin DeclareVars!(T, names);
}
struct S
{
mixin DeclareVars!(int, "x", "y");
}
void main()
{
import std.stdio;
auto s = S(1, 2);
writefln("x: %s, y: %s", s.x, s.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment