These are the functions I found as replacement for operators in the nix language:
let
n = 42; # number
n2 = 69
s = "foo"; # string
p = /path/to/foobar; # path
b = true; # boolean
l = ["foo" "bar"]; # list
a = {f = "foo";}; # attribute set
a2 = {b = "bar";};
in {
attributeSelection= __getAttr "f" a1 # a.f -> "foo"
negation = - n; # -n -> -42
# ^ technically not a function, because it has
# a lower operator precedence, but it suffices
hasAttribute = __hasAttr a2 s; # a ? s -> false
listConcatenation = __concatLists [l l] # l ++ l -> ["foo" "bar" "foo" "bar"]
multiplication = __mul n n; # n * n -> 1764
division = __div n n; # n / n -> 1
substraction = __sub n n; # n - n -> 0
addition = __add n n; # n + n -> 84
logicalNegation = ! b # !b
# ^ same as -, but even lower operator precedence
lessThan = __lessThan n n2; # n < n2 -> true
lessEqual = !(__lessThan n2 n) # n <= n2 -> true
greaterThan = __lessThan n2 n # n > n2 -> false
greaterEqual = !(__lessThan n n2) # n >= n2 -> false
}
So I’m still missing:
- {string,path} {path,string} concatenation (
+
on strings and paths) - update (
//
) - equality (
==
)- inequality is just ! equality
- logical AND (
&&
) - logical OR (
||
)- logical implication is just ! logical OR
- if then else constructs (
if condition then a else b
)
I know these are pretty easy to create:
{
# this is dirty i know
__concat = x: y:
if !(__isString x || __isPath x) then
abort "value is a ${__typeOf x} while a string or path was expected"
else if !(__isString y || __isPath y) then
abort "value is a ${__typeOf y} while a string or path was expected"
else x + y
;
__update = x: y: x // y;
__equal = x: y: x == y;
__and = x: y: x && y;
__or = x: y: x || y;
__ifThenElse = c: x: y: if c then x else y;
}
but that’s not what i meant