lua-users home
lua-l archive

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


>From manual:
"Different instances of the same function may refer to 
different
external local variables and may have different environment 
tables."

---
local function pr() print(s) end

local function th(a)
	setfenv(pr, setmetatable({s = a}, {__index = _G}))
	pr()
	coroutine.yield()
	pr()
end

local co1, co2 = coroutine.create(th), coroutine.create(th)
coroutine.resume(co1, 1)
coroutine.resume(co2, 2)
coroutine.resume(co1)
coroutine.resume(co2)
---
Prints:
1
2
2
2

Expecting:
1
2
1
2

I don't understand, why functions have own environment
and not use environment of current thread?