The OpenD Programming Language

naryFun

Aliases itself to a set of functions.

Transforms strings representing an expression into a binary function. The strings must use symbol names a, b, ..., z as the parameters. If functions[i] is not a string, naryFun aliases itself away to functions[i].

@optmath
template naryFun (
functions...
) if (
functions.length >= 1
) {}

Examples

// Strings are compiled into functions:
alias isEven = naryFun!("(a & 1) == 0");
assert(isEven(2) && !isEven(1));
alias less = naryFun!("a < b");
assert(less(1, 2) && !less(2, 1));
alias greater = naryFun!("a > b");
assert(!greater("1", "2") && greater("2", "1"));

naryFun accepts up to 26 arguments.

assert(naryFun!("a * b + c")(2, 3, 4) == 10);

naryFun can return by reference.

int a;
assert(&naryFun!("a")(a) == &a);

args parameter tuple

assert(naryFun!("args[0] + args[1]")(2, 3) == 5);

Multiple functions

alias fun = naryFun!(
    (uint a) => a,
    (ulong a) => a * 2,
    a => a * 3,
);

int a = 10;
long b = 10;
float c = 10;

assert(fun(a) == 10);
assert(fun(b) == 20);
assert(fun(c) == 30);

Meta