From a47a0f5da63cd28f5106a457128906958e68b4d3 Mon Sep 17 00:00:00 2001 From: Reuh Date: Wed, 27 Apr 2016 14:26:20 +0200 Subject: [PATCH] Added a separation between class.new and class:new --- changelog.txt | 2 ++ classtoi.lua | 32 +++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/changelog.txt b/changelog.txt index 9139b0d..c9dd2e7 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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: - Fixed a lot of bugs of the default class __tostring. 0.1.0: diff --git a/classtoi.lua b/classtoi.lua index d6c0bda..ea9ed33 100644 --- a/classtoi.lua +++ b/classtoi.lua @@ -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. -- Features: -- * 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 -- 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. - new = function(self, ...) - local obj = self() - -- Setting class methods to the ones found in parents (we use rawset in order to avoid calling the __newindex metamethod) - different = methods.new rawset(obj, "new", obj:__index("new") or nil) - different = methods["!is"] rawset(obj, "is", obj:__index("is") or nil) - 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 + -- A new object will only be created if calling the method "class:new(...)", if you call for example "class.new(someTable, ...)", it + -- will only execute the constructor defined in the class on someTable. This can be used to execute the parent constructor in a child + -- object, for example. + ["!new"] = function(self, ...) + if lastIndexed == self then + local obj = self() + -- Setting class methods to the ones found in parents (we use rawset in order to avoid calling the __newindex metamethod) + different = methods["!new"] rawset(obj, "new", obj:__index("new") or nil) + different = methods["!is"] rawset(obj, "is", obj:__index("is") or nil) + 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, --- 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.