lua-users home
lua-l archive

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


I came up with this experiment involving __index and __newindex to
segregate a table in its array part and non-array part:

https://gist.github.com/762890

Caveats:

It requires an special constructor to create the table:

  t = arrayTable{1, 2, a = 1}
  t[1.5]=1.5

It requires special iterators replacing both ipairs and pairs:

  for k,v in at_ipairs(t) do print(k, v) end
  for k,v in at_pairs(t) do print(k, v) end

Alternatively you can use the indexes to the real table/array provided
in the table

  for k,v in ipairs(t.___array) do print(k, v) end
  for k,v in pairs(t.___table) do print(k, v) end

Which I believe is even more ugly than using the iterators above.

I tried to override __len on the metatable but apparently that does
not work in lua 5.1 (I read somewhere __len will be overridable for
tables/strings in 5.2?)

--------------------------------------------------------------
EmmanuelOga.com - Software Developer