This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
theoriginalbit's profile picture

[Question] Metatable Events

Started by theoriginalbit, 13 January 2013 - 12:53 AM
theoriginalbit #1
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"
KaoS #2
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
theoriginalbit #3
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 :)/>
KaoS #4
Posted 13 January 2013 - 02:28 AM
no problem :)/> I tested that one and it works perfectly….