Skip to content

Instantly share code, notes, and snippets.

@majiang
Created November 10, 2014 13:08
Show Gist options
  • Save majiang/aa2e289846a1f703d6d3 to your computer and use it in GitHub Desktop.
Save majiang/aa2e289846a1f703d6d3 to your computer and use it in GitHub Desktop.
A function which receives the index and lazy arguments, and returns one of the arguments at the index.
import std.stdio;
/// won't compile.
auto choose(T)(size_t i, lazy T[] choice...)
{
return choice[i];
}
/// to be generalized.
auto choose(T)(size_t i, lazy T c0, lazy T c1)
{
return i ? c1 : c0;
}
auto arg(T)(T x)
{
"evaluated %d".writefln(x);
return x;
}
/// expecting exactly two lines of stdout.
void main()
{
"chose %d".writefln(choose(2, arg(0), arg(1), arg(2)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment