Am 09.01.2011 19:42, schrieb Andrei Alexandrescu:
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
This is exactly what I did today. In perl 6 it's called any which I think is a better name. There also 'none', 'all' and 'one'. There seems to be strange bug with the opEquals. Everything works but if you replace the opEquals template with it's actual instantiation dmd cry at you. See http://www.digitalmars.com/d/2.0/operatoroverloading.html#equals and my last post.

Reply via email to