lua-users home
lua-l archive

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


> 
> Any better suggestions?
> 

try setting a metatable for the table,...

__newindex, should work for new indexes
__index, should work if you are accessing already existing indexes

something along the lines of :


local tT = {}

setmetatable( tT, {
  __newindex = function( t, k, v )
    if v == nil then
      v = 'nil'
    else
      rawset(t, k, v )
    end,
  end,
  
  __index = function( t, k)
    if t[k] == nil then
      return 'nil'
    else
      rawget(t, k)
    end
  end
} )

table.insert( tT, foo )
table.insert( tT, baar )
table.insert( tT, baz )

a = b(t)