mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(class): recursive instanceOf and isClass method
This commit is contained in:
@@ -15,7 +15,9 @@ local function assertType(id, var, expected)
|
|||||||
local received = type(var)
|
local received = type(var)
|
||||||
|
|
||||||
if received ~= expected then
|
if received ~= expected then
|
||||||
error(("expected %s %s to have type '%s' (received %s)"):format(type(id) == 'string' and 'field' or 'argument', id, expected, received), 3)
|
error(
|
||||||
|
("expected %s %s to have type '%s' (received %s)"):format(type(id) == 'string' and 'field' or 'argument', id,
|
||||||
|
expected, received), 3)
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@@ -38,10 +40,23 @@ function mixins.new(class, 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.
|
||||||
function mixins.instanceOf(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.
|
||||||
|
function mixins.instanceOf(obj, class)
|
||||||
|
local mt = getmetatable(obj)
|
||||||
|
|
||||||
|
while mt do
|
||||||
|
if mt == class then return true end
|
||||||
|
|
||||||
|
mt = getmetatable(mt)
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
---Creates a new class.
|
---Creates a new class.
|
||||||
---@todo add inherited types for new/private/init fields (not yet supported by lls)
|
---@todo add inherited types for new/private/init fields (not yet supported by lls)
|
||||||
---@param name string
|
---@param name string
|
||||||
|
|||||||
Reference in New Issue
Block a user