either

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jan 9 10:42:22 PST 2011


I wrote a simple helper, in spirit with some recent discussions:

// either
struct Either(Ts...)
{
     Tuple!Ts data_;
     bool opEquals(E)(E e)
     {
         foreach (i, T; Ts)
         {
             if (data_[i] == e) return true;
         }
         return false;
     }
}

auto either(Ts...)(Ts args)
{
     return Either!Ts(tuple(args));
}

unittest
{
     assert(1 == either(1, 2, 3));
     assert(4 != either(1, 2, 3));
     assert("abac" != either("aasd", "s"));
     assert("abac" == either("aasd", "abac", "s"));
}

Turns out this is very useful in a variety of algorithms. I just don't 
know where in std this helper belongs! Any ideas?


Andrei


More information about the Digitalmars-d mailing list