lua-users home
lua-l archive

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


On 30.10.2011 15:39, Marc Balmer wrote:
Hi

Hi!


I am trying create a table with subtables, where subtables reference
each other, using the table constructor syntax:

a = {
   one = {
     a = 1,
     b = 2
   },
   two = {
     a = 1,
     b = one  -- set's b to nil
   }
}

Is there a way to write this constructor, so that a.two.b is set to
a.one instead of nil?  Or do I have to fixup the table after construction?

The best I can come up with is this:
$ cat > ref.lua
local temp = {}
local function v( s )
  return function( t )
    temp[ s ] = t
  end
end
local function r( s )
  return temp[ s ]
end

a = {
  one = v"one"{
    a = 1,
    b = 2
  },
  two = {
    a = 1,
    b = r"one"
  }
}

print( a.two.b.a )
^D

It seems to work, but I didn't find any guarantees about table constructor evaluation order by quickly skimming the ref manual ...


Thanks,
Marc


Philipp