lua-users home
lua-l archive

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


> meta =
> {
>   __metatable = false,
>   __tostring = function(o) return "hello " .. o.name end
> }
> 
> o = {}
> setmetatable(o, meta)
> o.name = "bob"
> print( o) -- yields "hello bob"
> o.name = "mick"
> print( getmetatable(o).__tostring(o)) -- causes "attempt to index a boolean value"

getmetatable(o) returns false and so the message is expected.

> print( o:__tostring()) -- causes "attempt to call method '__tostring' (a nil value)"

o does not have a __tostring method and so the message is expected.
Try adding this
	meta.__index = meta

The __tostring method is used by "tostring" and so print(o) works
even if the metatable is proteced.