Advertisement
throwawayrobot

lib/object.lua

Feb 24th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.52 KB | None | 0 0
  1. local function id(table)
  2.   return table.__id or (tostring(table) or ""):match(" ([%w]+)")
  3. end
  4.  
  5. local function class(metatable)
  6.   local metatable = metatable or {}
  7.   metatable.__id = id(metatable)
  8.   metatable.__index = metatable
  9.   metatable.__construct = metatable.__construct or function () end
  10.   return metatable
  11. end
  12.  
  13. local function object(metatable, ...)
  14.   local table = {}
  15.  
  16.   table.__id = id(table)
  17.   setmetatable(table, class(metatable))
  18.   table:__construct(...)
  19.   return table
  20. end
  21.  
  22. return object, id, class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement