Why do associative arrays return from opIndexAssign by value?

Jonathan M Davis jmdavisProg at gmx.com
Tue Jun 18 03:43:44 PDT 2013


On Tuesday, June 18, 2013 12:35:06 TommiT wrote:
> On Tuesday, 18 June 2013 at 10:18:23 UTC, Jonathan M Davis wrote:
> > On Tuesday, June 18, 2013 11:41:03 TommiT wrote:
> >> At least int assignment isn't lvalue. I find this quite
> >> surprising:
> >> 
> >> void edit(ref int) { }
> >> 
> >> void main()
> >> {
> >> 
> >>      int n;
> >>      //edit(n = 4); // Error
> >>      edit(n += 4);
> >> 
> >> }
> > 
> > Maybe that's by design, but it's not what I would have
> > expected. I don't know
> > if that's a bug or not. Certainly, it doesn't match how it
> > works in C++, and I
> > don't know why D would be any different in this regard.
> > 
> > - Jonathan M Davis
> 
> This must be by design, because associative arrays return lvalue
> from += operator as well:

Well, it's also the sort of thing that a lot of people screw up even in C++ - 
they make operater= return void when it should return a reference to the 
object being assigned to. So, it wouldn't entirely surprise me if it's the way 
that it is with AAs, because the person coding them up forgot to do otherwise. 
What's surprises me more is ints, but regardless of whether it's by design by 
not (and there's a decent chance that it is), I'd argue that it was a poor 
decision unless there's something that I'm missing.

In any case, I think that it's worth opening up an enhancement request.

- Jonathan M Davis


More information about the Digitalmars-d mailing list