xxxxxxxxxx
import std.stdio;
void when(T...)(T args)
{
static if (args.length > 0)
{
static if (args.length == 1) args[0]();
else
{
if(args[0]) args[1]();
else when(args[2..$]);
}
}
}
void myFunc()
{
writeln("last");
}
void main(string[] args)
{
bool c1 = false;
bool c2 = false;
// Syntax #1
when
(
c1, delegate() { writeln("first"); },
c2, delegate() { writeln("second"); },
delegate() { writeln("default"); }
);
c2 = true;
// Syntax #2
when
(
c1, () { writeln("first"); },
c2, () { writeln("second"); },
() { writeln("default"); }
);
c1 = true;
// Syntax #3
when
(
c1, () => writeln("first"),
c2, () => writeln("second"),
() => writeln("default")
);
c2 = false;
// Mixed!
when
(
c1, () => writeln("first"),
c2, delegate() { writeln("second"); },
&myFunc // also custom function
);
}