Revamping associative arrays

grauzone none at example.net
Sun Oct 18 14:34:54 PDT 2009


Piotrek wrote:
> bearophile Wrote:
> 
>> I'd really like the "default" iteration on an AA to yield its keys, instead of values as currently done. Because if I have a key I can find its value, while the opposite is not possible, so having keys is much more useful. This is true in Python too. In my dlibs all iterables and functions behave like this. The current D design is just a design mistake ad Walter was wrong on this.
>>
> 
> No! No! No! Maybe you are wrong. Or it's a metter of taste. I remember that I spent many hours on finding bug in python's script written by me in my job. The reason was the python's behaviour decribed by you. Then I couldn't understand why the hell iterating on collection returns a key in the first place. It's so not intuitive. Your explanation is not even close in convincing me. If I wanted keys I would write:
>  foreach (key, value; set) or (key; set.keys)
> 

In a perfect world, iterating over an AA would yield a tuple (key, 
value). You could iterate over either the keys or values by iterating 
over a "view" on the key or value list. I'm surprised Python doesn't do 
that.

(I'm expecting that Andrei will replace the .key and .value properties 
by "lazy" ranges, that don't allocate memory; so that aspect will be 
alright. But too bad the gods don't see the need for better tuple support.)



More information about the Digitalmars-d mailing list