原文
module foo;
mixin template opBi(A, A function(A, A)[string] f0,){
static foreach (k, f; f0) { A opBinary(string op: k)(A r) { return f(this, r); } }
}
struct A {
mixin opBi!(
A, [ "+": (A a, A b) => a],
);
}
struct B {
mixin opBi!(
B, [ "+": (B a, B b) => a],
);
}
你可以删除一些类型
并使用typeof(this)
.来传递
正确类型给f0
.
module foo;
mixin template opBi(alias f0){
static foreach (k, f; f0) { typeof(this) opBinary(string op:k)(typeof(this) r) { return f(this, r); } }
}
struct A {
mixin opBi!(
[ "+": (A a, A b) => a],
);
}
struct B {
mixin opBi!(
[ "+": (B a, B b) => a],
);
}
传入的AA
类型是T1[string]
,而期望的是T2[string]
,其中T1:B function(B, B) pure nothrow @nogc @safe
,T2:B function(B, B)
.