Calling function explicitly from mixin template results in recursive call instead

Dennis dkorpel at gmail.com
Sun Dec 9 18:36:50 UTC 2018


I'm using Adam's workaround from 
https://issues.dlang.org/show_bug.cgi?id=19365, but now I have 
endless recursion. Reduced code:

```
mixin template operators() {
     S opBinary(string op: "+")(S rhs) {
         return rhs;
     }

     // (A)
     auto opBinary(string op, T)(T rhs) if (false) {
         return rhs;
     }
}

struct S {
     mixin operators ops;
     S opBinary(string op, T)(T a) {
         return ops.opBinary!op(a);
     }
}

void main() {
     S.init.opBinary!"+"(S.init);
}
```

Believe it or not, `ops.opBinary!op(a);` doesn't call anything 
from the mixin template ops, but it calls itself and it results 
in a stack overflow. I think this is a bug, but last time I was 
wrong, so maybe someone can explain what's going on here.

Note that after removing the opBinary at (A), it works. The idea 
behind it is that it converts the rhs to an S that opBinary!"+" 
takes. A less reduced version would be:

```
auto opBinary(string op, T)(T rhs) if (!is(T==S)) {
     return this.opBinary!op(S(rhs));
}
```

Does anyone know how to get this working?


More information about the Digitalmars-d-learn mailing list