query
On this page

functionArgs

builtins.functionArgs

Primop
Docs pulled from | This Revision | about 2 hours ago


Nix manual

Takes 1 arguments

f

Return a set containing the names of the formal arguments expected by the function f. The value of each attribute is a Boolean denoting whether the corresponding argument has a default value. For instance, functionArgs ({ x, y ? 123}: ...) = { x = false; y = true; }.

"Formal argument" here refers to the attributes pattern-matched by the function. Plain lambdas are not included, e.g. functionArgs (x: ...) = { }.

Time Complexity

O(n) where n = number of formal arguments

Noogle detected

Detected Type
functionArgs :: (a) -> AttrSet

Implementation

This function is implemented in c++ and is part of the native nix runtime.

src/libexpr/primops.cc:3545

static void prim_functionArgs(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    state.forceValue(*args[0], pos);
    if (args[0]->isPrimOpApp() || args[0]->isPrimOp()) {
        v.mkAttrs(&Bindings::emptyBindings);
        return;
    }
    if (!args[0]->isLambda())
        state.error<TypeError>("'functionArgs' requires a function").atPos(pos).debugThrow();

    if (const auto & formals = args[0]->lambda().fun->getFormals()) {
        auto attrs = state.buildBindings(formals->formals.size());
        for (auto & i : formals->formals)
            attrs.insert(i.name, state.getBool(i.def), i.pos);
        /* Optimization: avoid sorting bindings. `formals` must already be sorted according to
           (std::tie(a.name, a.pos) < std::tie(b.name, b.pos)) predicate, so the following assertion
           always holds:
           assert(std::is_sorted(attrs.alreadySorted()->begin(), attrs.alreadySorted()->end()));
           .*/
        v.mkAttrs(attrs.alreadySorted());
    } else {
        v.mkAttrs(&Bindings::emptyBindings);
        return;
    }
}