lua-users home
lua-l archive

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


In lua 3.2:

Given:
  x={a=1, b=2}

  print(x.c)

yields 

  nil

as expected, but:

  print(x.b.e)

yields the error message:

  indexed expression not a table

I guess this makes sense, but for my application I would like a 
missing subfield to be considered nil, because I view x.b.e to really 
be a value reference of table x indexed by "b" and "e" (x["b"]["e"]), 
so that element is nil if "e" doesn't exist. I thought I could extend 
lua to handle those semantics by:

otm = settagmethod(tag(table), "gettable", gettable_event)

where gettable_event is the code from the manual

      function gettable_event (table, index)
             local tm = gettagmethod(tag(table), "gettable")
             if tm then
               return tm(table, index)
             elseif type(table) ~= "table" then
               error("indexed expression not a table");
             else
               local v = rawgettable(table, index)
               tm = gettagmethod(tag(table), "index")
               if v == nil and tm then
                 return tm(table, index)
               else
                 return v
               end
             end
           end

with the line:

            error("indexed expression not a table");

replaced with

            return nil

That caused a stack overflow with an infinite amounts of trace lines 
stating:

        `gettable' tag method [in file testtrap.lua]

Two questions:

1. What is the right way to implement the desired semantics?

2. Should my coding error have caused the continuous output 
described above, or is that a bug?

Thanks, in advance.

Stephen Rondeau
New lua programmer