naryFun

このテンプレートは、$(M std.functional.unaryFun)や$(M std.functional.binaryFun)を一般化し、 N個の引数を取れるようにしたものです。 $(M unaryFun)や$(M binaryFun)のように文字列に対して作用する場合は、その文字列で表される関数となります。 対して、文字列以外を$(M naryFun)に適用した場合には、その対象へのaliasとなります。

文字列による形式には、現在のところアルファベット及び数字に対応しています。

  1. auto ref naryFun(T args)
    template naryFun(alias fun, int N = -1)
    ref
    naryFun
    (
    T...
    )
    (
    auto ref T args
    )
    if (
    !(N >= 0) ||
    T.length == N
    )
    if (
    !is(typeof(fun) == string)
    )
  2. template naryFun(alias fun, int N = -1)

Members

Functions

naryFun
auto ref naryFun(T args)
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

alias test1 = naryFun!"a";
assert(test1(1) == 1);
assert(test1(test1(2.0)) == 2.0);
static assert(is(typeof({test1(1, 1);})));  // OK
                                            // 最初の引数を返す関数だから。
                                            // 2つ目の引数は使用されない。

static assert(!is(typeof({test1();})));     // NG


alias test1_1 = naryFun!("a", 1);       // 引数の数を1つとする
assert(test1_1(1) == 1);
assert(test1_1(test1(2.0)) == 2.0);
static assert(!is(typeof({test1_1(1, 1);})));  // NG


alias test1_2 = naryFun!("a", 2);       // 引数の数を2つとする
assert(test1_2(1, 1) == 1);
assert(test1_2(test1_2(2.0, 2), 1) == 2.0);
static assert(!is(typeof({test1_2(1);})));  // NG


alias test2 = naryFun!"b";
assert(test2(1, 2) == 2);
assert(test2(test2(1, "2"), test2(3.0, '4')) == '4');
static assert(!is(typeof({test2();})));
static assert(!is(typeof({test2(1);})));
static assert(is(typeof({test2(1, 1, 2.2);})));


// アルファベット
alias test3 = naryFun!"a + b + c";
assert(test3(1, 2, 3) == 6);

import std.bigint;
assert(test3(BigInt(1), 2, 3) == BigInt(6));


// 数字
alias test4 = naryFun!"_0 + _1 + _2 + _3";
assert(test4(1, 2, 3, 4) == 10);

Meta