Last active
December 17, 2015 12:19
-
-
Save JakobOvrum/5608585 to your computer and use it in GitHub Desktop.
tuplify - create tuple with named fields given a list of variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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