refactor(class): improve types

This commit is contained in:
Linden
2024-03-12 19:54:33 +11:00
parent 8180feaa8c
commit d105ebe7fe

View File

@@ -1,3 +1,4 @@
---@diagnostic disable: invisible
lib.print.warn('ox_lib\'s class module is experimental and may break without warning.') lib.print.warn('ox_lib\'s class module is experimental and may break without warning.')
---Ensure the given argument or property has a valid type, otherwise throwing an error. ---Ensure the given argument or property has a valid type, otherwise throwing an error.
@@ -20,6 +21,11 @@ local function assertType(id, var, expected)
return true return true
end end
---@class OxClass
---@field protected __name string
---@field protected private table
---@field protected super function
---@field protected constructor function
local mixins = {} local mixins = {}
local constructors = {} local constructors = {}
local getinfo = debug.getinfo local getinfo = debug.getinfo
@@ -40,8 +46,12 @@ end
local function void() return '' end local function void() return '' end
---Creates a new instance of the given class. ---Creates a new instance of the given class.
---@generic T
---@param class T
---@return T
function mixins.new(class, ...) function mixins.new(class, ...)
local obj local obj
---@cast class +OxClass
if table.type(...) == 'hash' then if table.type(...) == 'hash' then
lib.print.warn(([[Creating instance of %s with a table and no constructor. lib.print.warn(([[Creating instance of %s with a table and no constructor.
@@ -111,15 +121,20 @@ Use %s:constructor(...args) and assign properties in the constructor.]])
obj.private = nil obj.private = nil
end end
---@cast class -OxClass
return obj return obj
end end
---Checks if an object is an instance of the given class. ---Checks if an object is an instance of the given class.
---@param obj table
---@param class OxClass
function mixins.isClass(obj, class) function mixins.isClass(obj, class)
return getmetatable(obj) == class return getmetatable(obj) == class
end end
---Checks if an object is an instance or derivative of the given class. ---Checks if an object is an instance or derivative of the given class.
---@param obj table
---@param class OxClass
function mixins.instanceOf(obj, class) function mixins.instanceOf(obj, class)
local mt = getmetatable(obj) local mt = getmetatable(obj)
@@ -133,15 +148,16 @@ function mixins.instanceOf(obj, class)
end end
---Creates a new class. ---Creates a new class.
---@todo add inherited types for new/private/init fields (not yet supported by lls) ---@generic S : OxClass
---@param name string ---@param name string
---@param super? table ---@param super? S
function lib.class(name, super) function lib.class(name, super)
assertType(1, name, 'string') assertType(1, name, 'string')
local class = table.clone(mixins) local class = table.clone(mixins)
class.__name = name class.__name = name
---@diagnostic disable-next-line: inject-field
class.__index = class class.__index = class
if super then if super then
@@ -149,6 +165,7 @@ function lib.class(name, super)
setmetatable(class, super) setmetatable(class, super)
end end
---@todo See if there's a way we can auto-create a class using the name and super
return class return class
end end