Files
simple-makes-complex/mods/SYSTEM/Class/init.lua

31 lines
934 B
Lua

local setMetaTable, TableCopy = setmetatable, table.copy_with_metatables
---@class Class
Class = {}
---Returns a child class with inherited parameters from the parent class.
---@param parent table
---@param child table
function Class.extend(parent ,child)
return setmetatable(child or {}, { __index = TableCopy(parent) })
end
---Intended for class constructors.
---
---Correctly creates an object of the class using `setmetatable()`.
---@generic T:table
---@param self T Parent class.
---@param params table Child object parameters.
---@param metatable table? The table that will become the metatable for the object. Note: The `__index` field will be overwritten by a copy of `self`.
---@return T
function Class.getInstance(self, params, metatable)
if metatable == nil then
metatable = {}
end
metatable.__index = TableCopy(self)
local instance = setMetaTable(params, metatable)
return instance
end