www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What's opIndexAssign supposed to return ?

reply wjoe <invalid example.com> writes:
Lets say I've got 3 overloads of opIndexAssign:

auto opIndexAssign(T t);
auto opIndexAssign(T t, size_t i); and
auto opIndexAssign(T t, size_t[2] i);

I would assume to return what I would return with opIndex but I'd 
rather not act upon assumptions.
But if yes is it supposed to be the newly assigned values or the 
pre-assignment ones ? By value or by reference ? And if it's the 
new stuff can I just return t ?

The language manual on operator overloading didn't answer that 
question and neither did an internet search which didn't find any 
useful information. Something unrelated and a heads up about 
introducing opIndexAssign from 2004.
Feb 25 2020
next sibling parent reply Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Tuesday, 25 February 2020 at 11:02:40 UTC, wjoe wrote:
 Lets say I've got 3 overloads of opIndexAssign:

 auto opIndexAssign(T t);
 auto opIndexAssign(T t, size_t i); and
 auto opIndexAssign(T t, size_t[2] i);

 I would assume to return what I would return with opIndex but 
 I'd rather not act upon assumptions.
 But if yes is it supposed to be the newly assigned values or 
 the pre-assignment ones ? By value or by reference ? And if 
 it's the new stuff can I just return t ?

 The language manual on operator overloading didn't answer that 
 question and neither did an internet search which didn't find 
 any useful information. Something unrelated and a heads up 
 about introducing opIndexAssign from 2004.
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.
Feb 25 2020
parent wjoe <invalid example.com> writes:
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 :)
Feb 25 2020
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/25/20 3:02 AM, wjoe wrote:> Lets say I've got 3 overloads of 
opIndexAssign:
 auto opIndexAssign(T t);
 an internet search which didn't find any useful
 information.
I have examples for non-templatized and templatized versions of opIndexAssign here: http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.opIndexAssign http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.opIndexAssign%20template I used this index to find those: http://ddili.org/ders/d.en/ix.html Ali
Feb 25 2020
parent wjoe <invalid example.com> writes:
On Tuesday, 25 February 2020 at 15:30:19 UTC, Ali Çehreli wrote:
 On 2/25/20 3:02 AM, wjoe wrote:> Lets say I've got 3 overloads 
 of opIndexAssign:
 auto opIndexAssign(T t);
 an internet search which didn't find any useful
 information.
I have examples for non-templatized and templatized versions of opIndexAssign here: http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.opIndexAssign http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.opIndexAssign%20template I used this index to find those: http://ddili.org/ders/d.en/ix.html Ali
This is great! Thank you. Curious why this wasn't found by my internet search.
Feb 25 2020