'in' for arrays?

Lionello Lunesu lio at lunesu.remove.com
Wed Dec 6 08:06:16 PST 2006


Bill Baxter wrote:
> I keep wanting to say
>   if (val in somearray) {
>   }

Wait! That's just it!

It's "KEY in CONTAINER" and the KEY of a regular array is the index, not 
the value. So dare I suggest the following instead?

if (index in somearray) {
}

This "in" would be:
(cast(size_t)index < somearray.length?&somearray[index]:null)

Totally analogous to the associative array. And it also solves the 
"Length comparison" issue.

L.



More information about the Digitalmars-d mailing list