[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: global & unique
- From: "Zimmermann, Maik" <Zimmermann@...>
- Date: Wed, 21 Apr 2004 11:54:49 +0200
Hi,
the problem here is that you are using the same instance of pr
in both coroutines. Try the following:
--
local function th(a)
local function pr() print(s) end
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)
--
Hope that helps
Maik
> >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?
>