lua-users home
lua-l archive

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


I'm working on code with quite deep tables. When referring to an element
in the table, I use the table keys joined by dots, eg:

	a =

When one of the indicated elements does not exist, an error is generated
for apperent reasons: attempt to index nil value.

For my application, I'd like such an expression to return a nil value
instead of generating an error. Would it be possible to create such a
behaviour ? I've tried to create something like it with __index on
metatables - this avoids the errors, but returns the table itself
instead of nil :

  thing = {}
  b = {} = "blablah"
  b.__index = function(t, k)
    return thing
  setmetatable(thing, b)
  printf("%s\n", tostring(
  printf("%s\n", tostring(
  printf("%s\n", tostring(
Can anybody think of a solution for this ?