Class: add getInstance(). MapGen: fixes :new() methods. #18
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
local setMetaTable, TableCopy = setmetatable, table.copy_with_metatables
|
||||
|
||||
---@class Class
|
||||
Class = {}
|
||||
|
||||
@@ -5,5 +7,25 @@ Class = {}
|
||||
---@param parent table
|
||||
---@param child table
|
||||
function Class.extend(parent ,child)
|
||||
return setmetatable(child or {}, { __index = parent })
|
||||
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
|
||||
Reference in New Issue
Block a user