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;
}

Reply via email to