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

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Mar 21 15:52:58 PDT 2012


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.

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

Having said that, though, I'm a bit on the fence about making ++ or --
work in this case too. Those cases may be arguable because you need
something to exist before you can operate on it, so it may be OK to
throw in those cases. However, in the straight assignment case, I
definitely contend that it needs to work.


T

-- 
BREAKFAST.COM halted...Cereal Port Not Responding. -- YHL


More information about the Digitalmars-d mailing list