As discussed in DConf2015: Python-like keyword arguments
Atila Neves via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jun 4 10:37:16 PDT 2015
Compile-time version for when crazy people like me pass in values
as template parameters:
import std.stdio;
import std.array;
import std.typetuple;
import std.traits;
struct Foo { int i; }
struct Bar { int i; }
struct Baz { int i; }
void func(Foo foo = Foo(11), Bar bar = Bar(22), Baz baz =
Baz(33))() {
writeln("foo is ", foo);
writeln("bar is ", bar);
writeln("baz is ", baz);
}
void main() {
ctKwargs!(func, Bar(2), Baz(3), Foo(1));
writeln;
ctKwargs!(func, Baz(3), Foo(1));
}
auto ctKwargs(alias F, T...)() {
enum strArgs = getStrArgs!(F, T);
mixin("return F!(" ~ strArgs.join(",") ~ ")();");
}
template typesMatch(alias T) {
enum isRightType(alias U) = is(typeof(T) == typeof(U));
}
auto getStrArgs(alias F, T...)() {
string[] strArgs;
mixin("alias defaults = TemplateArgsOf!(" ~
F.stringof[0..$-2] ~ "!());");
foreach(value; defaults) {
enum ofRightType = Filter!(typesMatch!value.isRightType,
T);
static assert(ofRightType.length == 0 ||
ofRightType.length == 1,
text("Invalid type tuple ", T));
static if(ofRightType.length == 1) {
strArgs ~= ofRightType[0].stringof;
} else {
strArgs ~= value.stringof;
}
}
return strArgs;
}
More information about the Digitalmars-d
mailing list