5

引数の型がわからない関数を呼び出すためにタプルを作成する必要がある汎用関数があります。

このようなもの(arrayこの例では、外部コードによって作成されているため、関数を直接適用することはできません):

Result apply<Result, Where>(
    Anything[] array, 
    Callable<Result, Where> fun)
        given Where satisfies Anything[] => nothing;

このメソッドを実装し、指定された引数で呼び出される関数を取得するタイプセーフな方法はありますか?

4

2 に答える 2

2

This cannot be done completely type-safely... but assuming that the array indeed contains elements of the correct types as they should appear in a Tuple of type Where, the following function will do the trick:

Tuple<Anything, Anything, Anything> typedTuple({Anything+} array) {
    if (exists second = array.rest.first) {
        return Tuple(array.first, typedTuple({ second }.chain(array.rest.rest)));
    }
    else {
        return Tuple(array.first, []);
    }
}

And apply gets implemented as:

Result apply<Result, Where>(
    [Anything+] array, 
    Callable<Result, Where> fun)
        given Where satisfies Anything[] {
    value tuple = typedTuple(array);
    assert(is Where tuple);
    return fun(*tuple);
}
于 2015-12-08T19:03:15.160 に答える