Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
Last active December 17, 2015 12:19
Show Gist options
  • Save JakobOvrum/5608585 to your computer and use it in GitHub Desktop.
Save JakobOvrum/5608585 to your computer and use it in GitHub Desktop.
tuplify - create tuple with named fields given a list of variables
import std.typecons : Tuple;
import std.typetuple : TypeTuple;
template NameTypePairs(alias front, vars...)
{
private enum name = __traits(identifier, front);
private alias pair = TypeTuple!(typeof(front), name);
static if(vars.length == 0)
alias NameTypePairs = pair;
else
alias NameTypePairs = TypeTuple!(pair, NameTypePairs!vars);
}
auto tuplify(vars...)()
{
return Tuple!(NameTypePairs!vars)(vars);
}
auto func()
{
int x = 42, y = 13;
string z = "hi";
return tuplify!(x, y, z);
}
void main()
{
import std.stdio;
auto vars = func();
writefln("x: %s y: %s z: %s", vars.x, vars.y, vars.z);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment