lua-users home
lua-l archive

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


steve donovan wrote:
> Would be interesting to know the actual cost of allowing strings to
> have individual metatables.

consider this:

local a = setmetatable ("some string", metatable1)
local b = setmetatable ("some ", metatable2)
local c = setmetatable ("string", metatable2)

local d = b..c

if you forget about metatables, you'll find that a and d both are the _same_ string, and comparing them for equality is only a pointer comparison. that's possible because of string immutability.

but if each string can have a different metatable... are a and d still the _same_ string?  what metatable should it have?

even simpler:

local a = "anything"
local b = a
a = setmetatable (a, yourmetatable)

is b==a ?  what's the metatable of b?
if now a is a different value and b~=a, is a=="anything" still?

so, i think that immutable values have to share a single metatable for the whole type.

-- 
Javier