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

Jonathan M Davis jmdavisProg at gmx.com
Wed Mar 21 14:21:22 PDT 2012


On Wednesday, March 21, 2012 12:00:36 H. S. Teoh wrote:
> On Wed, Mar 21, 2012 at 07:29:44PM +0100, David Nadlinger wrote:
> > On Wednesday, 21 March 2012 at 18:27:30 UTC, H. S. Teoh wrote:
> > >A question was asked on the d-learn forum about why this throws a
> > >
> > >RangeError:
> > > int[string][int] map;
> > > map["abc"][20]++;
> > 
> > Wait a second – aren't AAs _supposed_ to throw if accessing a key
> > that doesn't exist yet? To be able to increment something, there
> > already has to be a value to start from…
> 
> [...]
> 
> If it doesn't exist, it should default to typeof(value).init, IMO.
> 
> 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. It's already bad enough IMHO that the language implicitly creates the 
AA itself (especially when you consider stuff like the bugs that occur when you 
pass an AA to a function expecting the elements that it adds to it to be in 
the AA afterwards - it works with an already initialized AA but not a null 
one).

I think that Steven's suggestion of a function for this makes far more sense.

- Jonathan M Davis


More information about the Digitalmars-d mailing list