[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: referencing table elements within the same table constructor
- From: Mark Hamburg <mark@...>
- Date: Thu, 10 Jun 2010 10:15:49 -0700
The function case works because it doesn't try to do the dereference until after the table exists. If the table is going into a local variable rather than a global, I think you may need to declare the variable and then assign it -- i.e.,
local interface
interface = {
port = 573495783,
showPort = function() print( "The port is:", interface.port ) end
}
The more general case of being able to reference values within the table while building the table is harder to define semantically and to implement and hence Lua passes on doing so. The short lived "in env do ... end" would have helped, but it would also have made it harder to access global variables from items within the construction code.
local xxx = { }
in construct( xxx, _G ) do
a = 10
b = a
end
Here "construct" creates a proxy table that does all assignments into the first parameter and chains through the full parameter list for global lookups.
But "in env do" is gone. We could, however, use the work3 formulation and do something like:
local xxx = construct_work3( { }, _G )( function( _ENV )
a = 10
b = a
end )
This does rather cry out for macros...
Mark