The difference between T[] opIndex() and T[] opSlice()

Paul Backus snarwin at gmail.com
Tue Oct 3 19:02:53 UTC 2023


On Tuesday, 3 October 2023 at 18:29:49 UTC, Salih Dincer wrote:
> More importantly, is there a priority order?  Because in our 
> last example, when we leave a single overload, all features are 
> executed through the ref opIndex except the bit:

The spec says:

> If an index expression can be rewritten using `opIndexAssign` 
> or `opIndexOpAssign`, those are preferred over `opIndex`.

Source: https://dlang.org/spec/operatoroverloading.html#array

So, the more specialized overloads are tried first, and the more 
general `opIndex` is tried last.

The only thing that's unclear here is the meaning of "can be 
rewritten"--as we've seen, the compiler only checks whether the 
specialized function *exists*, not whether it actually *works*, 
and will sometimes perform rewrites even when the resulting code 
does not compile.


More information about the Digitalmars-d-learn mailing list