Advertisement
osmarks

Metatable Madness

Feb 16th, 2019
1,830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.50 KB | None | 0 0
  1. local function new()
  2.     local x = { level = 1, new = new }
  3.     local m = {}
  4.     setmetatable(x, m)
  5.  
  6.     m.__eq = function(p1,p2)
  7.         if getmetatable(p1) == getmetatable(p2) then
  8.             return true
  9.         end
  10.     end
  11.  
  12.     m.__index = function(inst, key)
  13.         local lvl = rawget(inst, "level")
  14.         if key == "level" then
  15.             return lvl
  16.         else
  17.             return setmetatable({ level = lvl + 1 }, m)
  18.         end
  19.     end
  20.  
  21.     m.__tostring = function(inst)
  22.         return ("RECURSION "):rep(rawget(inst, "level"))
  23.     end
  24.    
  25.     return x
  26. end
  27.  
  28. return new()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement