In expression for array

Kirk McDonald kirklin.mcdonald at gmail.com
Fri Jan 25 01:55:53 PST 2008


Janice Caron wrote:
> On Jan 25, 2008 7:25 AM, bearophile <bearophileHUGS at lycos.com> wrote:
> 
>>- So using "in" makes code shorter, more flexible, more type-agnostic (generic), simpler to read, potentially faster and more standard :-)
> 
> 
> All good points. But I know that part of the reason it's a no-go is
> consistency. "in" has to scan the keys, not the values, or it would be
> inconsistent with AAs.
> 

Which doesn't stop Python from doing it this way. And it makes perfect 
sense. In an AA, you are typically concerned with whether a given key 
exists in the container. With an array, you are typically concerned with 
whether a given item is somewhere in the array. Arrays and associative 
arrays are fundamentally different, and I'm not sure this consistency 
actually buys us anything.

And, obviously, using 'in' to check whether a given index is in an array 
is basically useless. Using it as a containment check is /much/ more 
useful. So what if arrays and AAs act differently? You can't assign to 
an arbitrary non-existent index in an array, or append to an AA, either. 
They are not substitutes for each other.

> Personally I wouldn't mind dropping "in" in favor of two new infix
> functions "contains" and "containsKey", which would disambiguate the
> two cases (and give extra functionality for AAs). But it's probably
> too late to change things now, so forget I said anything. :-)

These can already be done as library functions, if there's that much 
concern about it.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org



More information about the Digitalmars-d mailing list