mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
feat(class): deprecate init in favour of constructor
This commit is contained in:
@@ -30,7 +30,18 @@ end
|
|||||||
local mixins = {}
|
local mixins = {}
|
||||||
|
|
||||||
---Creates a new instance of the given class.
|
---Creates a new instance of the given class.
|
||||||
function mixins.new(class, obj)
|
function mixins.new(class, ...)
|
||||||
|
local obj
|
||||||
|
|
||||||
|
if table.type(...) == 'hash' then
|
||||||
|
obj = ...
|
||||||
|
lib.print.warn(([[Creating instance of %s with a table and no constructor.
|
||||||
|
This behaviour is deprecated and will not be supported in the future.]])
|
||||||
|
:format(class.__name))
|
||||||
|
else
|
||||||
|
obj = {}
|
||||||
|
end
|
||||||
|
|
||||||
if obj.private then
|
if obj.private then
|
||||||
assertType('private', obj.private, 'table')
|
assertType('private', obj.private, 'table')
|
||||||
setmetatable(obj.private, private_mt)
|
setmetatable(obj.private, private_mt)
|
||||||
@@ -38,20 +49,27 @@ function mixins.new(class, obj)
|
|||||||
|
|
||||||
setmetatable(obj, class)
|
setmetatable(obj, class)
|
||||||
|
|
||||||
if class.init then
|
if class.constructor and obj ~= ... then
|
||||||
local parent = class
|
local parent = class
|
||||||
|
|
||||||
function obj:super(...)
|
function obj:super(...)
|
||||||
parent = getmetatable(parent)
|
parent = getmetatable(parent)
|
||||||
local superInit = parent and parent.init
|
local constructor = parent and parent.constructor
|
||||||
|
|
||||||
if superInit then return superInit(self, ...) end
|
if constructor then return constructor(self, ...) end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
obj:constructor(...)
|
||||||
|
elseif class.init then
|
||||||
|
lib.print.warn(([[Calling %s:init() is deprecated and will not be supported in the future.
|
||||||
|
Use %s:constructor(...args) and assign properties in the constructor.]])
|
||||||
|
:format(class.__name, class.__name))
|
||||||
|
|
||||||
obj:init()
|
obj:init()
|
||||||
obj.super = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
obj.super = nil
|
||||||
|
|
||||||
return obj
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user