Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. -- Example self-implementation
  2. local old_type = type
  3.  
  4. function type(object)
  5. if old_type(object) ~= "table" then
  6. return old_type(object)
  7. else
  8. local mt = getmetatable(object)
  9.  
  10. if old_type(mt) == "table" then
  11. if old_type(mt.__type) == "function" then
  12. return mt.__type(object)
  13. else
  14. return mt.__type
  15. end
  16. else
  17. return "table"
  18. end
  19. end
  20. end
  21.  
  22. -- This will work
  23. local ok_table = {
  24. __type = "OK table"
  25. }
  26.  
  27. setmetatable(ok_table, ok_table)
  28.  
  29. print(type(ok_table))
  30. -- > OK table
  31.  
  32. -- This will *not* work; setting __metatable blocks our new
  33. -- type() function from working
  34. local bad_table = {
  35. __type = "Bad table"
  36. __metatable = "No can has"
  37. }
  38.  
  39. setmetatable(bad_table, bad_table)
  40.  
  41. print(type(bad_table))
  42. -- > table (in this case)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement