lua-users home
lua-l archive

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


Hi

I was playing with environements and i dont understand this behavior.

local env1 = {print=print, a=1}
local env2 = {print=print, a=2}

local component = load([[
  item = {
    func = function()
      print(a)
    end,
    subItem = {
      func = function()
        print(a)
      end,
    }
  }
  ]])

component()

debug.setupvalue(item.func, 1, env1)
debug.setupvalue(item.subItem.func, 1, env2)

item.func()
item.subItem.func()

This print:
2
2

I was hoping it printed:
1
2

It would be great if someone could explain to me how env is propagated.

Thanks