lua-users home
lua-l archive

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


Hi list, I made a boring table module! I called it "xtable" because it's boring. It is available at https://bitbucket.org/SoniEx2/xtable

Features include useless read-only tables[1], erroring on missing keys, nil keys, nan keys, and nil values.

As written on the README:

local xtable = require "xtable"
local has, del = xtable.has, xtable.del -- for convenience
local t = xtable.new() -- create a new xtable
t[nil] = 1 -- nil key
t[0/0] = 2 -- nan key
t[1] = nil -- nil value
assert(has(t,1))
assert(#t == 1)
del(t,1) -- removing a key is not done with `t[k] = nil`
assert(#t == 0)
t = nil

local t = xtable.ctor({errormissing=true, readonly=true}) -- useless table
assert(not pcall(function() t[1] = 1 end)) -- read-only
assert(not pcall(function() return t[1] end)) -- error on missing keys

[1]: There's no way to pre-populate read-only tables. All read-only tables are fresh tables and contain no data. So, you can either have a table that errors if you try to read from it, or you can have a table that returns nil if you try to read from it. In either case, the table errors if you try to write to it.

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