lua-users home
lua-l archive

Re: Suggestion: Deprecate "Attempt to index a nil value" error; instead, return nil or create table

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

AltStyle によって変換されたページ (->オリジナル) /