boolean over multiple variables

strtr strtr at spam.com
Mon Jan 25 06:49:14 PST 2010


Simen kjaeraas Wrote:

> On Mon, 25 Jan 2010 09:59:42 +0100, Pelle MÃ¥nsson  
> <pelle.mansson at gmail.com> wrote:
> 
> > On 01/23/2010 12:29 AM, strtr wrote:
> >> Simen kjaeraas Wrote:
> >>
> >>>
> >>> Not tested, but they should work:
> >>>
> >>> if ( anySame( var, a, b, c, d ) ) {
> >>> }
> >>>
> >>> if ( allSame( var, a, b, c, d ) ) {
> >>> }
> >>>
> >>
> >> A lot prettier.
> >> I thought there would be a generic (basic) solution to this which I  
> >> just didn't know about but maybe I actually do know the basics by now :)
> >>> --
> >>> Simen
> >>
> > If we get opIn_r for arrays, you can do
> >
> > if (var in [a, b, c, d]) {
> > }
> >
> > Which I find a lot prettier.
> 
> Is this good enough?
> 
> struct CheckIf( T ) {
>    T payload;
>    bool opIn( T[] rhs ) {
>      foreach ( e; rhs ) {
>        if ( e == payload ) {
>          return true;
>        }
>      }
>      return false;
>    }
> }
> 
> CheckIf!( T ) checkIf( T )( T rhs ) {
>    return CheckIf!( T )( rhs );
> }
> 
> if ( checkIf( true ) in [ false, true, false ] ) {
> }
> 
> -- 
> Simen

Sometimes code just makes me smile :)


More information about the Digitalmars-d-learn mailing list