Which operators cannot be overloaded and why not?

Basile B. b2.temp at gmx.com
Mon Sep 13 15:06:02 UTC 2021


On Monday, 13 September 2021 at 14:42:42 UTC, jfondren wrote:
> On Monday, 13 September 2021 at 14:33:03 UTC, user1234 wrote:
>> - condition al expression ` cond ? exp : exp `
>
> And many other boolean operators, unary !, binary && and ||
>
> https://dlang.org/spec/operatoroverloading.html lists all the 
> overloadable operators, and 
> https://dlang.org/spec/expression.html has all the operators, 
> so "which operators" is a matter of close comparison. "why not" 
> is much harder to answer.

Oh! I have never noticed that `&&` and `||`, despite of being 
quite "ordinary" binary ops are not overloadable.

In 
[styx](https://styx-lang.gitlab.io/styx/attribute.html#operatorattribute) that works because, although inspired by the D way, in the sense that overloads are implemented in custom types, the selection is done using an expression template

```d
struct S {
     @operator(a && b) function andAnd(T other): auto {return 
false}
}
```

So as long as the expression in the attribute argument looks like 
a valid expression the stuff is found (and the supprot code in 
the compiler is super simple), e.g with code in a body:

```d
e1 && e2; // look if e1 is an aggregate and if it contains 
@operator(a && b)
e1 + e2;  // look if e1 is an aggregate and if it contains 
@operator(a && b)
```

While D uses specific identifier + templates value param for 
strings.
(note that only the equivalent of D opBinary**Right** works...)

anyway. zorry for this off-topic.


More information about the Digitalmars-d-learn mailing list