lua-users home
lua-l archive

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


On Wed, Apr 27, 2011 at 3:53 PM, Anthony Howe <achowe+lua@snert.com> wrote:
> In Lua 5.1 a thread has an environment and section 2.8 starts with
> "Every value in Lua can have a metatable." So a thread can also have a
> metatable in addition to an environment?
>
> In Lu 5.2 section 8.1 "Threads do not have individual environments. All
> threads share a single fixed environment."  Can a thread still have a
> metatable though?  Section 2.4 claims "Every value in Lua can have a
> metatable.".

Only tables and userdata have individual metatables, but other values
*can* have a metatable. In the case of numbers, this metatable is
shared across all numbers.

> debug.setmetatable(5, {__tostring = function(v) return string.format("num(%d)", v) end})
> print(4, 5, 6)
num(4)	num(5)	num(6)

The same applies to threads. You can use debug.setmetatable to set a
metatable on all coroutines, but they are shared across all
coroutines.

- Jim