Skip to content

Commit

Permalink
Add utility functions for wrapping falsy values
Browse files Browse the repository at this point in the history
needed for the implementation of the ternary operator to work properly
  • Loading branch information
generalmimon committed Apr 29, 2020
1 parent fc9556d commit 648ed1c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ function utils.byte_array_max(str)
return array_compare(str, function(x, y) return x > y end, bytes_subscript)
end

-- http://lua-users.org/wiki/TernaryOperator (section Boxing/unboxing, using functions)

local False = {}
local Nil = {}

function utils.box_wrap(o)
return o == nil and Nil or o == false and False or o
end

function utils.box_unwrap(o)
if o == Nil then return nil
elseif o == False then return false
else return o end
end

return utils

0 comments on commit 648ed1c

Please sign in to comment.