Jump to content




[Question] Metatable Events


  • You cannot reply to this topic
3 replies to this topic

#1 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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"

#2 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat 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 by KaoS, 13 January 2013 - 02:26 AM.


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 January 2013 - 02:26 AM

View PostKaoS, on 13 January 2013 - 02:15 AM, said:

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 :)

#4 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 13 January 2013 - 02:28 AM

no problem :) I tested that one and it works perfectly....





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users