lua-users home
lua-l archive

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


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.