Skip to content

alurm/lua-match

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Minimalistic sum types and switches for Lua

How? With a tagging function.

A minimal usage example:

local tag = dofile'tag.lua'
local a_rectangle = tag('rectangle', { width = 10, height = 20 })

local switch = {
  rectangle = function(s) return print('Got a rectangle shape with the width of ' .. s.width) end,
  -- An optional default case.
  [false] = function() return print('Shape type is not known') end,
}

-- Will print "Got a rectangle shape with the width of 10".
a_rectangle(switch)

For a more thorough example, see example.lua.

Tags are implemented by setting the __call metamethod on a table. The implementation (tag.lua) is so small, I'll just show it here in full:

return function(tag, value)
  setmetatable(value, {
    __call = function(value, switch)
      local case = switch[tag]
      if case then return case(value) end
      local default = switch[false]
      if default then return default() end
      error('no match for tag ' .. tag)
    end
  })
  return value
end

The license used for this project is MIT.

About

Minimalistic sum types and switches for Lua

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages