mixin template bug with opBinary?

Anthony Quizon anthoq88 at gmail.com
Fri Jul 22 12:33:37 UTC 2022


Hello,

I'm trying to create a mixin for quick binary operator overloads 
by passing in types with a corresponding associative array of 
strings to functions. However,
the code I currently have:

```
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],
     );
}


```

Will not let me override operators on both struct A and B.

I get:
```
foo.d(16): Error: mixin `foo.B.opBi!(B, ["+":function (B a, B b) 
pure nothrow @nogc @safe => a])` does not match template 
declaration `opBi(A, A function(A, A)[string] f0)`
```

Is this a bug or am I doing something wrong?


More information about the Digitalmars-d-learn mailing list