I'm sure there is a way, but it would most likely involve some hardcore table trickery, which I'd have to rack my brain for awhile to figure out.kikito wrote:Are you sure this is a problem due to closure usage? My gut tells me there must be a way to implement indirection with them too.BlackBulletIV wrote:Indeed, this is one big problem with closures, instances won't respond if you modify the class, which is a shame.
I was talking about this code inside _new (pasting below for reference)BlackBulletIV wrote:I'm not sure what you're talking about by copying methods directly to instances. If you're talking about the code the copies stuff from superclass to subclass, that's for inheriting class methods and attributes (and also modules).I'm not sure of what it does - maybe getting rid of the need of using : instead of . ? But then, why doing that only with module methods?Code: Select all
for _, m in pairs(cls.__modules) do for name, func in pairs(m) do if name ~= 'included' then self[name] = function(...) return func(self, ...) end -- get rid of self for the outside world end end end
Metamethods are a bit of a pain on lua when dealing with inheritance. The problem is that Lua doesn't want to use metatables in order to get metamethods: a table must have a __tostring metamethod on its metatable; if its "parent" (the metatable pointed by __index) has a __tostring, Lua doesn't care.BlackBulletIV wrote:I'm guessing metamethods are copied, I'm not quite sure how it works in MiddleClass (don't you just define an instance function). Anyway, metamethods are yet to be handled.
On middleclass I handle this by creating a set of "default metamethods" on every new class. These methods "point" to the methods on the superclass. And if there's no parent, they throw an error, just like a regular method would.
Oh that code copies module functions from the class into the instance. To maintain consistency it wraps the functions to remove the need of a self parameter. As explained in the recently added README, module functions must have a self parameter because they're not declared inside the initialisation function (the closure), therefore they need a reference to self.
Hmmmm, not quite sure whether I totally understand what you're talking about with metamethods. But I have seen how you make a default implementation which throws an error. But anyway, metamethods aren't implemented yet, they might be soon.
All in all, I think the closure method is not a very good fit for a full OOP system in Lua. But still, I love the style.