Mixin Templates and Operators

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 6 17:13:20 UTC 2022


On 4/6/22 6:36 AM, francesco.andreetto wrote:
> I have two structs, point and vec, in which I want to implement some 
> arithmetic operations.
> The operations are:
> 
> ```
> point - point = vec
> point + vec = point
> ```
> 
> Using mixin templates the code compiles but calling the operations in 
> the main causes an "incompatible type" Error:

This seems like a bug in the compiler. You can mixin operator overloads 
using one mixin template, but not multiple.

e.g. raylib-d uses this:

https://github.com/schveiguy/raylib-d/blob/89733bab9fd1d3588c14f4aa54b62ad45022a105/source/raymathext.d#L75

> 
> I tried to implement a single template:
> 
> ```d
> mixin template sumDiff(T, R){
>     R opBinary(string op)(T rhs) const
>     if (op == "+" || op == "-"){
>        return mixin("R(x " ~ op ~ " rhs.x, y " ~ op ~ "rhs.y, z " ~ op ~ 
> " rhs.z)");
>     }
> }
> ```
> 
> but the same error occurs.

This is different from your original. If I do this, it works:

```d
mixin template both(T, R) {
    R opBinary(string op : "+")(T rhs) const
    {
       return R(x + rhs.x, y + rhs.y, z + rhs.z);
    }

    T opBinary(string op : "-")(R rhs) const
    {
       return T(x - rhs.x, y - rhs.y, z - rhs.z);
    }
}

struct point{
    float x, y, z;
    mixin both!(vec, point);
}
```
> 
> If I don't use the mixin templates and, instead, I overload the 
> operations into the structures everything works.

It also works to call `opBinary` directly:

```d
vec v1 = p1.opBinary!"-"(p2);
```

Which leads me to believe it's not an ambiguity error, but rather just a 
straight omission on how the operator overloading works.

I think you should file a bug.

-Steve


More information about the Digitalmars-d-learn mailing list