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

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 21 13:07:22 PDT 2012


On Wed, 21 Mar 2012 14:29:14 -0400, H. S. Teoh <hsteoh at quickfur.ath.cx>  
wrote:

> A question was asked on the d-learn forum about why this throws a
> RangeError:
>
> 	int[string][int] map;
> 	map["abc"][20]++;
>
> This is understandable, since the compiler translates the second line to:
>
> 	map.opIndex("abc").opIndexUnary!"++"(20);
>
> Since map["abc"] doesn't exist yet, opIndex throws RangeError before we
> ever get to the ++.

What about a create accessor of the map which is the same as accessing the  
map, except it ensures T.init is used at each level?

That is:

map.create["abc"][20]++;

Whether you want default-created elements or not is an important design  
decision that should not be up to the container.

-Steve


More information about the Digitalmars-d mailing list