Revamping associative arrays

Bill Baxter wbaxter at gmail.com
Sun Oct 18 13:33:45 PDT 2009


On Sun, Oct 18, 2009 at 1:12 PM, Piotrek <starpit at tlen.pl> wrote:
> Bill Baxter pisze:
>>
>> I think the default should be to iterate over whatever 'in' looks at.
>>
>
> I was almost convinced, because that rule has a sense. But treating normal
> arrays and associative array has more sense to me.
>
>
> fun (SomeObject object) {
> foreach (element;object.arr1){ //normal, but how do I know at first look
> //just do something with element
> }
>
> foreach (element;object.arr2){ // assoc, but how do I know at first look
> //just do something with element hopefully not index
> }

That sounds like an argument that there should be no default, because
either way it's not clear whether you're iterating over keys or
values.  That's reasonable too, I think.  Just get rid of the the
one-argument foreach over AAs altogether and force the user to be
explicit about it.  Probably much less error-prone than quietly
changing the D1 default, for sure. :-)   As much as people go on about
making it easy to port C code, really ya gots to think about all the
D1 code too.  It shouldn't be harder to port D1 code to D2 than C
code!

So for a new language I would go for what I said before.  But for D, I
think the better move is to get rid of the one-arg foreach and require
.keys / .values explicitly.  (And make that efficient, of course).

--bb



More information about the Digitalmars-d mailing list