chore: class new is now generic

This commit is contained in:
Alexander Courtis
2024-10-28 14:00:52 +11:00
parent 5647bc3c7c
commit 9b365279df
18 changed files with 26 additions and 24 deletions

View File

@@ -1,22 +1,24 @@
---Generic class, useful for inheritence.
---@class (exact) Class
---@field private __index? table
local Class = {}
---@param o Class?
---@return Class
---@generic T
---@param self T
---@param o T|nil
---@return T
function Class:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
self.__index = self ---@diagnostic disable-line: inject-field
return o
end
---Object is an instance of class
---This will start with the lowest class and loop over all the superclasses.
---@param class table
---@generic T
---@param class T
---@return boolean
function Class:is(class)
local mt = getmetatable(self)
@@ -32,7 +34,7 @@ end
---Return object if it is an instance of class, otherwise nil
---@generic T
---@param class T
---@return `T`|nil
---@return T|nil
function Class:as(class)
return self:is(class) and self or nil
end