Skip to content

Commit

Permalink
Add isPath primop
Browse files Browse the repository at this point in the history
this is added for completeness' sake since all the other possible
`builtins.typeOf` results have a corresponding `builtins.is<Type>`
  • Loading branch information
zimbatm committed Mar 24, 2019
1 parent 56f1ed5 commit 514b3c7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/manual/expressions/builtins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,14 @@ x: x + 456</programlisting>

</varlistentry>

<varlistentry><term><function>builtins.isPath</function>
<replaceable>e</replaceable></term>

<listitem><para>Return <literal>true</literal> if
<replaceable>e</replaceable> evaluates to a path, and
<literal>false</literal> otherwise.</para></listitem>

</varlistentry>

<varlistentry xml:id='builtin-isNull'>
<term><function>isNull</function>
Expand Down
7 changes: 7 additions & 0 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ static void prim_isBool(EvalState & state, const Pos & pos, Value * * args, Valu
mkBool(v, args[0]->type == tBool);
}

/* Determine whether the argument is a path. */
static void prim_isPath(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
state.forceValue(*args[0]);
mkBool(v, args[0]->type == tPath);
}

struct CompareValues
{
Expand Down Expand Up @@ -2169,6 +2175,7 @@ void EvalState::createBaseEnv()
addPrimOp("__isInt", 1, prim_isInt);
addPrimOp("__isFloat", 1, prim_isFloat);
addPrimOp("__isBool", 1, prim_isBool);
addPrimOp("__isPath", 1, prim_isPath);
addPrimOp("__genericClosure", 1, prim_genericClosure);
addPrimOp("abort", 1, prim_abort);
addPrimOp("__addErrorContext", 2, prim_addErrorContext);
Expand Down
2 changes: 2 additions & 0 deletions tests/lang/eval-okay-types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ with builtins;
(isFloat (1 - 2.0))
(isBool (true && false))
(isBool null)
(isPath /nix/store)
(isPath ./.)
(isAttrs { x = 123; })
(isAttrs null)
(typeOf (3 * 4))
Expand Down

0 comments on commit 514b3c7

Please sign in to comment.