The OpenD Programming Language

either

Get the first argument a that passes an if (unaryFun!pred(a)) test. If no argument passes the test, return the last argument.

Similar to behaviour of the or operator in dynamic languages such as Lisp's (or ...) and Python's a or b or ... except that the last argument is returned upon no match.

Simplifies logic, for instance, in parsing rules where a set of alternative matchers are tried. The first one that matches returns it match result, typically as an abstract syntax tree (AST).

CommonType!(T, Ts)
either
(
alias pred = a => a
T
Ts...
)
(,
lazy Ts alternatives
)
if (
alternatives.length >= 1 &&
!is(CommonType!(T, Ts) == void)
&&
)

Return Value

Type: CommonType!(T, Ts)

The first argument that passes the test pred.

Bugs

Lazy parameters are currently, too restrictively, inferred by DMD to always throw even though they don't need to be. This makes it impossible to currently mark either as nothrow. See issue at https://issues.dlang.org/show_bug.cgi?id=12647.

Examples

const a = 1;
const b = 2;
auto ab = either(a, b);
static assert(is(typeof(ab) == const(int)));
assert(ab == a);

auto c = 2;
const d = 3;
auto cd = either!(a => a == 3)(c, d); // use predicate
static assert(is(typeof(cd) == int));
assert(cd == d);

auto e = 0;
const f = 2;
auto ef = either(e, f);
static assert(is(typeof(ef) == int));
assert(ef == f);
immutable p = 1;
immutable q = 2;
auto pq = either(p, q);
static assert(is(typeof(pq) == immutable(int)));
assert(pq == p);

assert(either(3, 4) == 3);
assert(either(0, 4) == 4);
assert(either(0, 0) == 0);
assert(either("", "a") == "");
string r = null;
assert(either(r, "a") == "a");
assert(either("a", "") == "a");

immutable s = [1, 2];
assert(either(s, s) == s);

assert(either([0, 1], [1, 2]) == [0, 1]);
assert(either([0, 1], [1]) == [0, 1]);
assert(either("a", "b") == "a");

static assert(!__traits(compiles, either(1, "a")));
static assert(!__traits(compiles, either(1.0, "a")));
static assert(!__traits(compiles, either('a', "a")));

Meta