'in' for arrays?

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Dec 6 06:59:05 PST 2006


"Egor Starostin" <egorst at gmail.com> wrote in message 
news:el5vpc$9nt$1 at digitaldaemon.com...
> 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).

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.  :) 





More information about the Digitalmars-d mailing list