Comparing Multiple Values

Bill Baxter dnewsgroup at billbaxter.com
Tue Mar 11 14:28:21 PDT 2008


BCS wrote:
> Reply to Derek,
> 
>> On Tue, 11 Mar 2008 13:39:55 +0100, downs wrote:
>>
>>> There's a better way actually.
>>>
>> But that is only good for ranges ;-)
>>
>> Not so good for ...
>>
>> if value in set(2,4,7,10,23,445)
>>
>> I'm pretty sure this will be simple to organize once AST macros are
>> implemented.
>>
> 
> struct _Set(T) {
>  T[] set;
>  bool opIn_r(U)(U u) {
>    foreach(v; set) if(v==u) return true;
>    return false;
>  }
> }
> 
> struct Set {
>  static _Set!(T) opIndex(T, U)(T[] a...) {
>    _Set!(T) ret;
>    ret.set = a.dup; //need the dup???
>    return ret;
>  }
> }
> 
> I /think/ that will work. But I haven't tested it.
> 
> if(1 in Set[1,2,3,5,9,23])

Hadn't noticed that you wrote basically the same thing I did already ...

You left a U template parameter in the opIndex.
The dup isn't necessary.
And unfortunately it doesn't work because IFTI isn't smart enough to 
deduce the template argument.  So you need to call it as Set[[1,2,3,4]].

--bb


More information about the Digitalmars-d-learn mailing list