7508 posts
Location
Australia
Posted 13 January 2013 - 01:53 AM
Does anyone know if there is a way to do something to the effect of
object.__type
or is there anyway to add in so the following would work
local myObj = object:new()
print( type( myObj ) )
would print something like say "object"
1548 posts
Location
That dark shadow under your bed...
Posted 13 January 2013 - 02:15 AM
there is no metatable for the type function, you can modify the type function to check the metatable though… it is a messy workaround and will not work for protected metatables either
EDIT: added functionality to run the mt.__type if it is a function
EDIT2: simplified the code
local oldT=type
type=function(param)
if oldT(param)=="table" then
local mt=getmetatable(param)
if oldT(mt)=="table" and oldT(mt.__type)=="function" then
return mt.__type(param)
elseif oldT(mt)=="table" and mt.__type then
return mt.__type
end
end
return oldT(param)
end
as I said… messy
Edited on 13 January 2013 - 01:26 AM
7508 posts
Location
Australia
Posted 13 January 2013 - 02:26 AM
there is no metatable for the type function, you can modify the type function to check the metatable though… it is a messy workaround and will not work for protected metatables either
EDIT: added functionality to run the mt.__type if it is a function
local oldT=type
type=function(param)
if param and oldT(param)=="table" then
local mt=getmetatable(param)
if oldT(mt)=="table" and mt.__type and oldT(mt.__type)=="function" then
return mt.__type(param)
elseif oldT(mt)=="table" and mt.__type then
return mt.__type
end
end
return oldT(param)
end
as I said… messy
Damn just as I thought… I tried something to the effect of that and it didn't quite work how I wanted it to… I have another way to get to the desired outcome, just using type would have been nicer… oh well… thanks for the help :)/>
1548 posts
Location
That dark shadow under your bed...
Posted 13 January 2013 - 02:28 AM
no problem :)/> I tested that one and it works perfectly….