query
On this page

add

builtins.add

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


Nix manual

Takes 2 arguments

e1, e2

Return the sum of the numbers e1 and e2.

Noogle detected

Aliases

Detected Type
add :: Number -> Number -> Number

Implementation

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

src/libexpr/primops.cc:4439

static void prim_add(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
    state.forceValue(*args[0], pos);
    state.forceValue(*args[1], pos);
    if (args[0]->type() == nFloat || args[1]->type() == nFloat)
        v.mkFloat(
            state.forceFloat(*args[0], pos, "while evaluating the first argument of the addition")
            + state.forceFloat(*args[1], pos, "while evaluating the second argument of the addition"));
    else {
        auto i1 = state.forceInt(*args[0], pos, "while evaluating the first argument of the addition");
        auto i2 = state.forceInt(*args[1], pos, "while evaluating the second argument of the addition");

        auto result_ = i1 + i2;
        if (auto result = result_.valueChecked(); result.has_value()) {
            v.mkInt(*result);
        } else {
            state.error<EvalError>("integer overflow in adding %1% + %2%", i1, i2).atPos(pos).debugThrow();
        }
    }
}