dmd
dmd copied to clipboard
mixin templates cannot produce opDispatch or opBinary overloads with multiple mixins
import std.stdio;
mixin template OD(string s){
void opDispatch(string name)() if(name == s){
writeln("op dispatch for ", name);
}
}
struct T {
mixin OD!"x";
mixin OD!"y";
}
struct U {
mixin OD!"z";
}
void main(){
T t;
//t.x(); //error!
//t.y(); //error!
t.opDispatch!"x";
t.opDispatch!"y";
U u;
u.z(); //OK
}
The fact that explicitly calling opDispatch works for T but that the dispatches themselves don't work makes this seems like a simple oversight rather than a necessary limitation.
Not an expert, but I think the error might be happening because the T struct mixes in opDispatch twice, and both have the same signature. It's kinda weird tho, I don't disagree.
Yes, but they have different template constraints, so should make a valid overload set. If you copy-pasted bodies of the mixin templates instead of using mixin it works fine.
I believe if U.z compiles, T.x, and T.y should also compile.
Note that this is different from overloading with regular member functions (which mixin templates never do).
And also, it seems it's not just opDispatch, any operator overload requires only one overload from a mixin template.
template adder() {
int opBinary(string s : "+")(int x) { return x; }
}
template subtracter() {
int opBinary(string s : "-")(int x) { return x; }
}
struct S
{
mixin adder;
mixin subtracter;
}
void main()
{
S s;
s + 5; // error
s - 5; // error
}
Updating title accordingly.