lua-users home
lua-l archive

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


On Sun, Nov 29, 2009 at 12:26:45PM +0100, spir wrote:
> Is there a way to let a table item refer to another one:
>    t = {x=1, y=x+1}
> or
>    t = {x=1, y=t.x+1}
> (at table creation time).
> Meaning let a table act as a scope/namespace/module.

If you ignore your parenthetical qualification, yes, you can make a
table be a namespace.

function foo()
    local t = {x=1}
    setfenv(1, t)   -- now t provides the global namespace inside foo
    t.y = x         -- now t.y == t.x == 1
    return t
end

However, you say you want to do that all at once -- to have later
entries in the table literal detect the meaning of earlier entries. I
can't imagine what use case would demand that, but as far as I can tell,
no that's not possible. Not without hacking the Lua sources.

I may be wrong, but I don't think I am.

-- 
Jim Pryor
profjim@jimpryor.net