On Jul 6, 2016, at 9:06 AM, Soni L. <fakedme@gmail.com> wrote:
On 06/07/16 05:15 AM, steve donovan wrote:
On Wed, Jul 6, 2016 at 8:25 AM, steve donovan <steve.j.donovan@gmail.com> wrote:
in place then the `n` field becomes an implementation detail. The
table functions start respecting this, and nirvana is achieved.
Alas, but freedom from the wheel of death and rebirth is always harder
than it first seems.
The table returned from table.pack() now has a metatable (for __len)
and inevitably it will be passed to Lua code which doesn't respect
this. For example, pl.List will take a table (_assumed_ to be a
sequence) and slap its own metatable on it, rebranding it as a List
[0]. The carefully-crafted __len is lost in translation.
[0] the name is the result of initial Python-nostalgia, which has faded.
table.list(t) -> setmetatable(t, listmt); local i = 0; for k,v in pairs(t) do if math.type(k) == "integer" and k > i then i = k end end t._N = i --[[ alternatively, put it in the registry in a weak table, to avoid conflicts with user keys ]]
table.remove(t) -> if debug.getmetatable(t) == listmt then t._N = t._N-1 end --[[ remove key and move everything over 1 ]]
table.insert(t) -> if debug.getmetatable(t) == listmt then t._N = t._N+1 end --[[ move everything over 1 and add key ]]
listmt -> {__newindex=--[[function to update _N if the new key is bigger than current _N]], __len=function(t) return t._N end, __metatable="list"} --[[ this would be stored in the registry, not sure if there'd be a way to un-listify a list ]]
This would probably be the easiest way to do it such that some tables would act like arraylists, it'd also replace that module you're talking about. However, for performance reasons, we'd need a way to mass-remove and mass-insert keys. (as in _N = _N-count instead of for i=1,count do _N=_N-1 end)