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].


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


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


- - -
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

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

-- "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()
- - -


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.