mixin template bug with opBinary?
Steven Schveighoffer
schveiguy at gmail.com
Fri Jul 22 13:07:13 UTC 2022
On 7/22/22 8:33 AM, Anthony Quizon wrote:
> 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?
It's typing the AA differently, and therefore it doesn't fit. The type
of the AA you are passing in is `T1[string]`, where it's expecting
`T2[string]`, where:
`T1` is `B function(B, B) pure nothrow @nogc @safe`
`T2` is `B function(B, B)`
I don't know if there's a better way to do this, other than use a
further template parameter to match the function type passed in.
-Steve
More information about the Digitalmars-d-learn
mailing list