What's opIndexAssign supposed to return ?

wjoe invalid at example.com
Tue Feb 25 12:26:29 UTC 2020


On Tuesday, 25 February 2020 at 11:49:50 UTC, Petar Kirov 
[ZombineDev] wrote:
> On Tuesday, 25 February 2020 at 11:02:40 UTC, wjoe wrote:
>> [...]
>
> opIndexAssign is the operator used in the following code:
>
> arr[1] = 8;
>
> It returns the element at index 1 (so 8 in this case) by 
> reference.
> This allows you to do:
>
> (arr[1] = 8)++;
> assert(arr[1] == 9);
>
> Whether or not you want to support this behavior in your custom 
> data structure is up to you. It's perfectly valid to return the 
> element by value or even return void.
>
> Returning void from any custom assignment operator is always a 
> safe choice. It's possible that some algorithms (e.g. in Phobos 
> or third-party libraries) may need op*Assign to return 
> something, but in that unlikely case you'll get a compile-time 
> error, so it will be an easy fix.

Excellent answer ! Thanks for your fast reply :)


More information about the Digitalmars-d-learn mailing list