forked from blitmap/lua-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friendly-nil.lua
executable file
·34 lines (28 loc) · 1.37 KB
/
friendly-nil.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env lua
-- make nil friendlier
-- these functions are named in the form of what they return
local none = function ( ) end
local nnil = function (l, r) return l ~= nil and l or r end
local nil_mt =
{
__call = none, -- call a nil value without pcall and have it do nothing!
__index = none, -- stop writing: return some_table and some_table[idx] or ...
__concat = nnil, -- stop concatenating to an initial empty-string when iteratively building a long string!
__add = nnil, -- stop adding nil to things...
__unm = function ( ) return - 0 end, -- sign is preserved even on 0
__mul = function (l, r) return 0 * nnil(l, r) end, -- again, to preserve signedness
__sub = function (l, r) return - nnil(l, r) end, -- just unary-minus the rhs
__div = function (l, r) return 0 / nnil(l, r) end, -- we can't make use of zero() because 0 / 0
__pow = function (l, r) return 0 ^ nnil(l, r) end,
}
debug.setmetatable(nil, nil_mt)
local a = a()
local b = b['something']
local c = c .. 'testing'
local d = d + 3
local e = -e
local f = f * -3 -- signage test
local g = g - 3
local h = h / 0 -- nan test
local i = i ^ -3 -- inf test
print(a, b, c, d, e, f, g, h, i)