More magical AA semantics

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Jan 12 08:10:42 PST 2013


On Sat, Jan 12, 2013 at 04:40:16AM -0800, Jonathan M Davis wrote:
> On Saturday, January 12, 2013 13:30:47 monarch_dodra wrote:
[...]
> > What about "++a[0]" when there is no a[0]? Is this something that
> > will throw or not?
> 
> Personally, I think that it should, but there's probably a good chance
> that it won't, because it's an lvalue. I don't know what will happen
> with that though.  There are a lot of bugs right now related elements
> being inserted into AAs when they shouldn't be, so I really don't know
> how much relation the current behavior will have with the behavior
> that AAs will ultimately have.

Yeah, like:

	http://d.puremagic.com/issues/show_bug.cgi?id=3825
	http://d.puremagic.com/issues/show_bug.cgi?id=4463
	http://d.puremagic.com/issues/show_bug.cgi?id=5234
	http://d.puremagic.com/issues/show_bug.cgi?id=6178
	http://d.puremagic.com/issues/show_bug.cgi?id=8170


> > As well, what about
> > //----
> > immutable(int)[int] aa;
> > aa[0] = 5;
> > //----
> > This should work, right?
> 
> That should definitely work. That's how you add elements to AA. My
> gripe with something like ++a[0] working when there's no a[0] is that
> you'd be adding to an element that doesn't exist yet.
[...]

I think issue 4463 shows that ++a[0] produces 1.0 for a double[int],
which is totally wrong (it should be nan, if it's even allowed in the
first place).

The main problem is that the current AA code in aaA.d has no information
on the value types, so a lot of things are done by binary instead of by
type information. So .init is wrongly conflated with the equivalent of
memset(0), opEquals is wrongly conflated with binary equality, etc.,
etc.. Basically, it's a fractal of brokenness that just happens to work
in enough common cases that it barely avoids being unusably frustrating.


T

-- 
Skill without imagination is craftsmanship and gives us many useful
objects such as wickerwork picnic baskets.  Imagination without skill
gives us modern art. -- Tom Stoppard


More information about the Digitalmars-d mailing list