[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Suggestion: Deprecate "Attempt to index a nil value" error; instead, return nil or create table
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Wed, 4 Mar 2020 06:42:42 -0300
> - 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.