[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Access a member from within the table def
- From: steve donovan <steve.j.donovan@...>
- Date: Fri, 14 May 2010 13:15:19 +0200
2010/5/14 spir ☣ <denis.spir@gmail.com>:
> Another occurrence of "executable table defs" ;-)
Always more than one way to do anything...
local init -- to make sure the function is visible within itself
function init(val)
local t = {}
setfenv(init,t)
-- we're now writing directly to t, and t's fields are available on the RHS
x = val
y = x+10
z = x+y
return t
end
local t = init(10)
print(t.x,t.y,t.z)
=>
10 20 30
OK, a little awkward, and in fact Lua 5.2's with..do will do this more
elegantly. But it does show the principle
steve d.