d重写参数理念
这只是一种假想.
调用foo(x),在x有opArgs成员时,重写为foo(x.opArgs).同样,可对称的,如果定义foo(T x),T有opArgs成员时,重写为foo(typeof(T.opArgs)x).
该功能太强大,你可以这样:
struct S {
string opArgs;
}
string fun(S s) { return "S重载"; }
string fun(string s) { return "串重载"; }
void main() {
assert(fun(S()) == "S重载"); //失败了
}
这不是自动扩展,概念上多参函数与带一个元组参数函数一样.
void foo(int a, string b){}
// pattern (int a,string b)
foo(1,"2"); // 匹配(1,"2")元组
(int a, string b) = (1,"2"); //同样
void foo((int a, string b), double c){}
foo((1,"2"), 3.0);
((int a,string b), double c) = ((1,"2"),3.0);
可以这样:
alias apply(alias fun) = (args) => fun(args.tupleof);
[(1,2), (3,4)].map!(apply!((a, b) => a+b)).each!writeln;
但没人愿意,你可以简单alias S=AliasSeq!(string);这样.
浙公网安备 33010602011771号