lua-users home
lua-l archive

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


> - You can't have fields called `_count`, `_dimension`, `_idx`, or `_back`. This is minor. You could replace them with UUIDs such that a user would have to go out of their way to collide with them.

You can avoid special fields like that by using a separate table for
private attributes:

local Sparse = {}
local Attr = setmetatable({}, {__mode = “k”})

local function new(dimension)
   local sparse = {}
   Attr[sparse] = { dimension = dimension, count = 1 }
...
   if Attr[sparse].count == Attr[sparse].dimension then
...

Note that Attr is a weak table. This technique is mentioned in our
CACM paper, "A Look at the Design of Lua"
<https://cacm.acm.org/magazines/2018/11/232214-a-look-at-the-design-of-lua/>.
See Figure 6.