3 ms·
... Until you encounter a case where false is a valid value, in which case it’s ~= nil all over again. (Tip: on LuaJIT, x == nil is also true for FFI pointers t
by mananaysiempre 4y ago
... Until you encounter a case where false is a valid value, in which case it’s ~= nil all over again. (Tip: on LuaJIT, x == nil is also true for FFI pointers that happen to be null, which can sometimes force an FFI temporary to be allocated; using type(x) == 'nil' instead may help.)
If your language supports a genuine nil and not just a distinguished singleton token (AFAIK only Lua does that consistently, though perhaps “undefined” in non-strict-mode JavaScript also counts), I’m actually leaning towards having operators for dealing with it specifically. Icon does something like that, arguably, although its behaviour (for a “failing” expression that yields no values) is more like a propagating NaN / relational NULL / poison value than a nil.
Nix (though it has a faux null like everybody else, as well as a poisoning bottom typical of lazy languages) has a slick partial solution: the field access operator is optionally ternary, expr.name [or expr], so you can write things like
systems.flakeExposed or systems.supported.hydra
when you want to fall back from one to the other, even though in the fallback case the value of systems.flakeExposed by itself is not nil, it’s an error. (Using or outside of field access is a syntax error, the Boolean operator is spelled ||.)
- ok_dad 4y agoGood point, I still have to check nils on some stuff in Lua, but way way less than I do in my Python day job! I liked Kotlin's null-handling when I worked in it because there were some tools there to handle nulls in a reasonable manner. I think the `something?.prop` would do null checking on `something` before trying to access `prop` and then you could use `?:` (Elvis operator, haha) to check a null and do something if it was null, like `something ?: executed_if_null` and combining those would effectively deal with nulls in a simple and readable way.