lua-users home
lua-l archive

Re: [Proposal] rawpairs()

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


It was thus said that the Great Soni L. once stated:
> pairs() is considered harmful, and can cause bugs: 
> https://github.com/MightyPirates/OpenComputers/issues/1625
 Could not reproduce.
[spc]lucy:/tmp>cat y.lua
local t = {}
local mt = {
 __index = {"1", "2", "3", test="hi"}, -- index 
 __gc = function() end -- dummy __gc, to trigger bug
}
local mtmt = {
 __pairs = function() -- dummy __pairs that returns an empty iterator
 return next, {}, nil
 end
}
setmetatable(mt, mtmt) -- needs to be done first
setmetatable(t, mt) -- trigger bug
-- ok so now let's test our __index shall we?
print(t[1], t[2], t[3], t.test) -- nil, nil, nil, nil
-- wait what?! it doesn't work!
[spc]lucy:/tmp>lua-51 y.lua
1	2	3	hi
[spc]lucy:/tmp>lua-52 y.lua
1	2	3	hi
[spc]lucy:/tmp>lua-53 y.lua
1	2	3	hi
[spc]lucy:/tmp>
 My Lua 5.1 and 5.2 are the latest version, but I think I'm still a bit
behind on 5.3 (5.3.0), so that may have some effect. But otherwise ... what
exactly is going on?
 -spc

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