Please inspect the code below to see some strange behavior :
[code]
local t = {1,2,3,4,5}
local f = function() return 5 end
local n = 5
local s = "five"
local B = {
["NUM"] = getmetatable(n),
["STR"] = getmetatable(s),
["TAB"] = getmetatable(t),
["FUN"] = getmetatable(f)
}
local function IsMType(anyValue,sType)
if(getmetatable(anyValue) == B[sType]) then
return true
end
return false
end
print("MT Number : "..tostring(IsMType(n,"NUM")),"NUM")
print("MT Number : "..tostring(IsMType(n,"STR")),"STR")
print("MT Number : "..tostring(IsMType(n,"FUN")),"FUN")
print("MT Number : "..tostring(IsMType(n,"TAB")),"TAB")
print("MT String : "..tostring(IsMType(s,"NUM")),"NUM")
print("MT String : "..tostring(IsMType(s,"STR")),"STR")
print("MT String : "..tostring(IsMType(s,"FUN")),"FUN")
print("MT String : "..tostring(IsMType(s,"TAB")),"TAB")
print("MT Function : "..tostring(IsMType(f,"NUM")),"NUM")
print("MT Function : "..tostring(IsMType(f,"STR")),"STR")
print("MT Function : "..tostring(IsMType(f,"FUN")),"FUN")
print("MT Function : "..tostring(IsMType(f,"TAB")),"TAB")
print("MT Table : "..tostring(IsMType(t,"NUM")),"NUM")
print("MT Table : "..tostring(IsMType(t,"STR")),"STR")
print("MT Table : "..tostring(IsMType(t,"FUN")),"FUN")
print("MT Table : "..tostring(IsMType(t,"TAB")),"TAB")
[/code]
Output:
MT Number : true
MT Number : false
MT Number : true
MT Number : true
MT String : false
MT String : true
MT String : false
MT String : false
MT Function : true
MT Function : false
MT Function : true
MT Function : true
MT Table : true
MT Table : false
MT Table : true
MT Table : true
As you see here the so called types "number", "function", "table" have the same meta table... Is this normal ?
Because getmetatable on numbers, functions and standard tables return nil.
String does not because it has a metatable (eg. "SomeString":lower())
Sorry, you need to Log In to post a reply to this thread.