Another attempt at passing keyword-arguments to functions

Tomer Filiba via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 9 04:11:29 PST 2015


What do you guys think?

struct KW(alias F) {
     private import std.string, std.range, std.traits, 
std.typecons, std.algorithm;
     mixin({
         string s;
         foreach(i, name; ParameterIdentifierTuple!F) {
             static assert ((ParameterStorageClassTuple!F[i] & 
ParameterStorageClass.out_) == 0, "Cannot pass 'out' parameters");
             static assert ((ParameterStorageClassTuple!F[i] & 
ParameterStorageClass.ref_) == 0, "Cannot pass 'ref' parameters");
             s ~= fullyQualifiedName!(ParameterTypeTuple!F[i]) ~ " 
" ~ name ~ ";\n";
         }
         return s;
     }());
     private static KW _instance;

     @disable this();
     @disable this(this);

     static auto opIndex(T...)(ref T _) {
         static assert (T.length == 
ParameterIdentifierTuple!F.length);
         mixin("return F(" ~ 
ParameterIdentifierTuple!F.tuple.array.map!(p => "_instance." ~ 
p).join(",") ~ ");");
     }

     static KW* opDollar(int dim)() {
         return &_instance;
     }
}

auto f(int x, string y) {
     return x*2 + y.length;
}

void main() {
     auto res = KW!f[$.y = "foo", $.x = 5];

     import std.stdio;
     writeln(res);     // 13
}


-tomer


More information about the Digitalmars-d mailing list