'in' for arrays?

Bill Baxter dnewsgroup at billbaxter.com
Wed Dec 6 00:59:44 PST 2006


Egor Starostin wrote:
>> I keep wanting to say
>>    if (val in somearray) {
>>    }
> Me too.
> 
> For example, in Python I can write
> 
> if val in ('off','disable','no')
> 
> and in D I have to write
> 
> if (val == "off" || val == "disable" || val == "no")
> 
> which is less readable (to me, at least).
> 
> 
> --
> Egor

Yep.  That's the kind of usage case I have in mind.

About "it's expensive but it doesn't look expensive"...
Expensive is all relative.  If you tend to have lists with tens or even 
hundreds of elements in them then a linear search is a perfectly 
reasonable way to do it.  And in terms of "looks inexpensive", well to 
me   (val in somearray) looks exactly like it's going to do a linear 
search.  What else *could* it do when handed a general array?

--bb



More information about the Digitalmars-d mailing list