lua-users home
lua-l archive

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


If I want to protect a metatable with an opaque value, by setting its __metatable field to non-nil, which is the best value to use?

The value true seems reasonable.

if getmetatable(t) then -- this would pass
    getmetatable(t)[k] = foo -- but then this would be bad
end

The value false also seems reasonable. It makes the above code OK, but means you'd have to explicitly check against nil to determine whether a metatable exists.

if getmetatable(t) ~= nil then -- this would pass
    print(getmetatable(t)) -- this is OK
    getmetatable(t)[k] = foo -- this is still bad
end

Anyways, I was just wondering if there is a reason to prefer one over the other. Or if another value is best. I don't care about debugging values or anything, so I don't need a string. Just opaque protection of the metatable.

What do you use?

Marc