www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - One old problem with associative arrays

reply "bearophile" <bearophileHUGS lycos.com> writes:
import std.stdio;
void main() {
     int[] a;
     int[int] aa;
     a ~= a.length;
     aa[10] = aa.length;
     writeln(a, " ", aa);
     a ~= a.length;
     aa[20] = aa.length;
     writeln(a, " ", aa);
}


It prints:

[0] [10:1]
[0, 1] [20:2, 10:1]

So the associative array is updated before taking its length. I 
suggest to try to fix this AA behavour before too much D2 code 
relies on this. I'd like to avoid this to become a permanent wart 
of D2.

My bug report is from February 2010, it's in the most important 
20 bug reports of mine. In my opinion it's wrong to add new 
features to D before similar bugs are fixed:

http://d.puremagic.com/issues/show_bug.cgi?id=3825

Thank you,
bye,
bearophile
Nov 06 2012
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Nov 07, 2012 at 05:08:43AM +0100, bearophile wrote:
[...]
 So the associative array is updated before taking its length. I
 suggest to try to fix this AA behavour before too much D2 code relies
 on this. I'd like to avoid this to become a permanent wart of D2.
 
 My bug report is from February 2010, it's in the most important 20
 bug reports of mine. In my opinion it's wrong to add new features to
 D before similar bugs are fixed:
 
 http://d.puremagic.com/issues/show_bug.cgi?id=3825

I'd love to fix AA's once for all. The problem is that right now the AA code is scattered all over the compiler, and even within druntime it's a schizophrenic mosaic of half-duplicated code, partly in aaA.d and partly in object_.d. It's a major undertaking even just to refactor this code in a sane way. Would you like to help in this effort? ;-) I tried to do it myself some time ago, and I did get as far as a struct that works pretty much like the built-in AA's with quite a good number of current issues fixed, but it got stuck with handling const, pure, IFTI bugs, and a number of related issues. Frankly, at this point I believe that it's going to take more than one person to do it. We need a team of about 2-3 to tackle different aspects of it so that we can clean up the whole thing properly. My code is already up on github, but so far it seems that no one has been interested to contribute yet. I do agree that cleaning up AA's should be quite an important item in D2. There are quite a lot of IMO serious problems with the current implementation. But it ain't gonna happen unless more people help. T -- Meat: euphemism for dead animal. -- Flora
Nov 06 2012
parent Don Clugston <dac nospam.com> writes:
On 07/11/12 05:38, H. S. Teoh wrote:
 On Wed, Nov 07, 2012 at 05:08:43AM +0100, bearophile wrote:
 [...]
 So the associative array is updated before taking its length. I
 suggest to try to fix this AA behavour before too much D2 code relies
 on this. I'd like to avoid this to become a permanent wart of D2.

 My bug report is from February 2010, it's in the most important 20
 bug reports of mine. In my opinion it's wrong to add new features to
 D before similar bugs are fixed:

 http://d.puremagic.com/issues/show_bug.cgi?id=3825

I'd love to fix AA's once for all. The problem is that right now the AA code is scattered all over the compiler, and even within druntime it's a schizophrenic mosaic of half-duplicated code, partly in aaA.d and partly in object_.d. It's a major undertaking even just to refactor this code in a sane way. Would you like to help in this effort? ;-) I tried to do it myself some time ago, and I did get as far as a struct that works pretty much like the built-in AA's with quite a good number of current issues fixed, but it got stuck with handling const, pure, IFTI bugs, and a number of related issues. Frankly, at this point I believe that it's going to take more than one person to do it. We need a team of about 2-3 to tackle different aspects of it so that we can clean up the whole thing properly. My code is already up on github, but so far it seems that no one has been interested to contribute yet. I do agree that cleaning up AA's should be quite an important item in D2. There are quite a lot of IMO serious problems with the current implementation. But it ain't gonna happen unless more people help. T

I keep getting hurt by the bizarre "magic null" that happens with AAs. It makes the implementation really horrible. Can we please please make it so that declaring an AA creates an empty AA? So that AAs are always reference types. (It would also be OK if they had to explicitly created, just like classes are; but these semantics of "inserting an item into a null AA creates the AA first" is really awful). Making AAs nicer and less bug-prone for users would make them easier to implement!
Nov 07 2012
prev sibling parent kenji hara <k.hara.pg gmail.com> writes:
2012/11/7 H. S. Teoh <hsteoh quickfur.ath.cx>:
[snip]
 I tried to do it myself some time ago, and I did get as far as a struct
 that works pretty much like the built-in AA's with quite a good number
 of current issues fixed, but it got stuck with handling const, pure,
 IFTI bugs, and a number of related issues.

If you tell me which bugs should be fixed for the new AA implementation, I'll try to fix them. Kenji Hara
Nov 07 2012