Files
simple-makes-complex/mods/SYSTEM/Class/init.lua
2026-11-17 21:35:05 +03:00

33 lines
974 B
Lua

local tableMerge, setmetatable, tableCopy = table.merge, 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 tableMerge(child or {}, 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