is opOpAssign returning a value less than ideal ?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Thu Nov 8 09:54:09 UTC 2018
On Thursday, November 8, 2018 2:15:43 AM MST Codifies via Digitalmars-d-learn
wrote:
> On Thursday, 8 November 2018 at 06:01:57 UTC, Jonathan M Davis
>
> wrote:
> > On Wednesday, November 7, 2018 10:45:07 PM MST Jonathan M Davis
> >
> > via Digitalmars-d-learn wrote:
> >> [...]
> >
> > Rereading what you wrote, are you asking whether it's
> > reasonable to return a value instead of a reference?
> > Personally, I don't think that that's good design at all, but I
> > also don't see any reason for the compiler to prevent it.
> >
> > Personally, I think that the default design should be to return
> > by ref. Returning void is less than ideal but isn't necessarily
> > bad, depending on the situation (especially if we're not
> > talking about a general purpose library). However, I expect
> > that returning non-void by value rather than by ref is rarely
> > -if ever - going to be a good design choice. It's just going to
> > be confusing and not particularly useful.
> >
> > - Jonathan M Davis
>
> NB its not returning a ref to the list, its returning the newly
> created node when you use the operator you add a type to the
> list, so it possibly wouldn't be that much use for chaining?
The normal thing to do with any of the assignment operators is to return a
reference to the object you're assigning to. Doing much of anything else
other than returning void (and thus not allowing chaining) would be pretty
abnormal. If it's appropriately documented, having an assignment operator
return something else could certainly be made to work, but I expect that
most programmers would consider it to be a backwards API.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list