is opOpAssign returning a value less than ideal ?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Nov 8 05:45:07 UTC 2018


On Wednesday, November 7, 2018 9:28:19 PM MST Codifies via Digitalmars-d-
learn wrote:
> I noticed that opOpAsign allows you to return a value...
>
> this means I can do this (return a node from my list class when
> adding a new node)
> ```
> anode = alist ~= &someData;
> ```
> to me this looks a little unusual (but to be fair I can live with
> it)
>
> being as when its used like this:
> ```
> alist ~= &someData;
> ```
> you need to find out what the ~ operator does anyway, I don't
> think it harms readability
>
> any thoughts?

It's common practice in C++ to have the various assignment operators return
a reference to the object being assigned to so that the operations can be
chained, and I don't see why the situation in D would be any different. But
if for whatever reason, you don't want to do the same with your types, you
can always just make those operators void. Either way, the fact that an
assignment operator returns a reference to the object doesn't require you to
then actually use it for anything. It just allows folks to do so if they
think that it's appropriate in a particular piece of code. Sometimes, it's
useful; often it isn't, but if the return type is void, then you can't do it
even in the cases where it would be useful.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list