!in

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Feb 17 11:43:34 PST 2010


Michel Fortin wrote:
> On 2010-02-17 12:07:05 -0500, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> said:
> 
>> Searching a value in a literal should actually be allowed:
>>
>> x in [10, 20, 30, 0]
>>
>> is great because the compiler has complete discretion in how to 
>> conduct the search (e.g. linear vs. binary vs. hash search; it can 
>> take the initiative of presorting the literal). But general search in 
>> an unstructured range... maybe not.
> 
> Are you talking about literals or compile-time constants? A literal can 
> be built using variables and functions, such as:
> 
>     x in [a, b, c, d, e]
> 
> This would be mostly equivalent to this:
> 
>     x == a || x == b || x == c || x == d || x == e
> 
> I'd tend to allow it as it makes it easier to write and read 
> conditionals with repeated comparisons against the same variable.

I was thinking CTFE-able array literals, but I think linear search in a 
short array (in a way all array literals are O(1)) is fair game too.

> But I guess that's less important than supporting compile-time constants:
> 
>     const allowedCharacters = ['0','1','2','3','4','5','6','7','8','9'];
> 
>     if (x in allowedCharacters)
>         ...;
> 
> 

Indeed.


Andrei



More information about the Digitalmars-d mailing list