'in' for arrays?

Pragma ericanderton at yahoo.removeme.com
Thu Dec 7 07:58:25 PST 2006


Tuples, For The Win.

This is a prime example of why we can overload operators in D, and how 
templates further enhance that capability.  There's little need to add 
language features when the language supports so many possible constructs 
already.

Jarrett Billingsley wrote:
>
> struct _Tuple(T...)
> {
>  bool opIn_r(U)(U value)
>  {
>   foreach(val; T)
>   {
>    static if(is(typeof(val) : U))
>    {
>     if(val == value)
>      return true;
>    }
>   }
> 
>   return false;
>  }
> }
> 
> template Tuple(T...)
> {
>  const _Tuple!(T) Tuple;
> }
> 
> void main()
> {
>  if(4 in Tuple!("hi", 5.5, 4))
>   writefln("yep");
>  else
>   writefln("nope");
> 
>  if("no" in Tuple!("hi", 5.5, 4))
>   writefln("yep");
>  else
>   writefln("nope");
> }
> 
> 
> Meh.  :) 
> 
> 


-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list