Wrong lowering for a[b][c]++

Jonathan M Davis jmdavisProg at gmx.com
Wed Mar 21 15:58:07 PDT 2012


On Wednesday, March 21, 2012 15:52:58 H. S. Teoh wrote:
> On Wed, Mar 21, 2012 at 05:21:22PM -0400, Jonathan M Davis wrote:
> > On Wednesday, March 21, 2012 12:00:36 H. S. Teoh wrote:
> [...]
> 
> > > But perhaps that was the wrong example to use. What if you wanted to
> > > do this:
> > > 
> > > map["abc"][20] = 1;
> > > 
> > > Currently this doesn't work. You have to explicitly create
> > > map["abc"] first.
> > 
> > Which I think makes perfect sense. I think that implicitly creating
> > elements is evil.
> 
> [...]
> 
> I disagree. If you wrote:
> 
> if (map["abc"][2] == 1)
> 
> then I agree that this should throw a RangeError.
> 
> But in the case of writing map["abc"][20]=1, you're explicitly saying
> "assign 1 to map["abc"][20]". This means the user *wants* to create a
> new entry in the AA. So it should create the intermediate entry
> necessary as well.
> 
> Otherwise, by your reasoning, this should fail too:
> 
> int[string] aa;
> aa["abc"] = 1; // should throw RangeError since entry doesn't exist
> 
> which I think reduces the usability of the indexing syntax.

Of course that shouldn't throw. You're explicitly creating the element in the 
AA.

> Having the opIndexAssign syntax work only when there's a single level of
> indexing seems to me completely arbitrary.

Not at all. Until you assign the a value to the key in the AA, there's no 
second level to do anything with. To do anything else would be to use the 
value before it's been added to the AA.

- Jonathan M Davis


More information about the Digitalmars-d mailing list