Files
simple-makes-complex/mods/SYSTEM/Class/init.lua
2026-02-06 17:14:02 +03:00

31 lines
935 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