Which operators cannot be overloaded and why not?

Steven Schveighoffer schveiguy at gmail.com
Mon Sep 13 15:03:30 UTC 2021


On 9/13/21 10:47 AM, user1234 wrote:
> On Monday, 13 September 2021 at 14:33:03 UTC, user1234 wrote:
>> what else ?
> 
> when you have
> 
> ```d
> alias AA1 = int[int];
> alias AA2 = AA1[int];
> ```
> 
> then you can write
> 
> ```d
> AA2 aa;
> aa[0] = [0 : 0];
> aa[0][0] = 0;
> ```
> 
> The `[0][0]` cannot be expressed using operator overloads (and a custom 
> map type that implements opIndexAssign).But this case is rather due to 
> the fact that druntime seems to do something a bit unusal here 
> (according to an old discussion that happend once on irc).
> 

This is because the compiler calls a different hook depending on the 
usage of the expression `aa[0]`. Which one it calls is not consistent.

There isn't an analog for indexing overloads.

A further example:

```d
int[int] aa;
aa[0]++; // ok
void foo(ref int x) {x++;}
foo(aa[1]); // range violation
```

-Steve


More information about the Digitalmars-d-learn mailing list