lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Typecheck is a Lua module for checking types. It uses annotation-style checks and depends on APow[1].

Repo: https://bitbucket.org/TeamSoni/typecheck

It was inspired by `nil` being a separate type in Lua.

Features:

- Coroutine tricks.
- Annotation-style API.
- Return-style typechecks.
- Non-nil by default.

Examples:

- - -
local tc = require "typecheck"

-- Simple checks, error() on invalid type
-- AKA "unchecked exception" style

local function mustbetable(t)
  t = tc.uncheck "table" ^ t
  return t.n or #t
end

local function tableornil(t)
  t = tc.uncheck "table" "nil" ^ t
  return t and (t.n or #t) or 0
end

-- "Checked exception" style, returns `nil, errmsg` on invalid type
-- (note the use of tc.checked)

local mustbeopenfile = tc.checked ^ function(f)
  f = tc.check(io.type, "file") ^ f
  return f:seek()
end
- - -

[1]: https://bitbucket.org/TeamSoni/apow

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.