1
0
Fork 0
mirror of https://github.com/Reuh/classtoi.git synced 2025-10-27 12:19:31 +00:00

Added a separation between class.new and class:new

This commit is contained in:
Reuh 2016-04-27 14:26:20 +02:00
parent 9412583311
commit a47a0f5da6
2 changed files with 23 additions and 11 deletions

View file

@ -1,3 +1,5 @@
0.1.2:
- Calling class.new(table, ...) now call the class constructor on the table instead of creating a new object.
0.1.1: 0.1.1:
- Fixed a lot of bugs of the default class __tostring. - Fixed a lot of bugs of the default class __tostring.
0.1.0: 0.1.0:

View file

@ -1,4 +1,4 @@
--- Reuh's class library version 0.1.1. Lua 5.1-5.3 and LuaJit compatible. --- Reuh's class library version 0.1.2. Lua 5.1-5.3 and LuaJit compatible.
-- Objects and classes behavior are identical, so you can consider this to be prototype-based. -- Objects and classes behavior are identical, so you can consider this to be prototype-based.
-- Features: -- Features:
-- * Multiple inheritance with class(parents...) or someclass(newstuff...) -- * Multiple inheritance with class(parents...) or someclass(newstuff...)
@ -55,16 +55,26 @@ methods = {
-- The same happens with :new and :is, but since they're not metamethods, if not defined in a parent you won't -- The same happens with :new and :is, but since they're not metamethods, if not defined in a parent you won't
-- notice any difference. -- notice any difference.
-- TL;DR (since I think I'm not really clear): you can redefine __call, :new and :is in parents and use them in objects only. -- TL;DR (since I think I'm not really clear): you can redefine __call, :new and :is in parents and use them in objects only.
new = function(self, ...) -- A new object will only be created if calling the method "class:new(...)", if you call for example "class.new(someTable, ...)", it
local obj = self() -- will only execute the constructor defined in the class on someTable. This can be used to execute the parent constructor in a child
-- Setting class methods to the ones found in parents (we use rawset in order to avoid calling the __newindex metamethod) -- object, for example.
different = methods.new rawset(obj, "new", obj:__index("new") or nil) ["!new"] = function(self, ...)
different = methods["!is"] rawset(obj, "is", obj:__index("is") or nil) if lastIndexed == self then
different = methods.__call rawset(obj, "__call", obj:__index("__call") or nil) local obj = self()
different = nil -- Setting class methods to the ones found in parents (we use rawset in order to avoid calling the __newindex metamethod)
-- Call constructor different = methods["!new"] rawset(obj, "new", obj:__index("new") or nil)
if obj.new ~= methods.new and type(obj.new) == "function" then obj:new(...) end different = methods["!is"] rawset(obj, "is", obj:__index("is") or nil)
return obj different = methods.__call rawset(obj, "__call", obj:__index("__call") or nil)
different = nil
-- Call constructor
if obj.new ~= methods.new and type(obj.new) == "function" then obj:new(...) end
return obj
else
different = methods["!new"]
local new = lastIndexed:__index("new") or nil
different = nil
return new(self, ...)
end
end, end,
--- Returns true if self is other or a subclass of other. --- Returns true if self is other or a subclass of other.
-- If other is nil, will return true if self is a subclass of the class who called this method. -- If other is nil, will return true if self is a subclass of the class who called this method.