Page 15 of 25
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Wed Jan 26, 2011 1:19 am
by leiradel
What happens if you have method "a" in a class and the class includes a mixin which has also an "a" method when using middleclass? Which one will instances use?
I'm starting to think I can switch from my own OOP implementation back to middleclass...
Cheers,
Andre
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Wed Jan 26, 2011 2:06 am
by BlackBulletIV
I know in PHP 5.4 (they call mixins traits) handles that situation by allowing the user to choose:
Code: Select all
class Foo
{
use TraitA, TraitB
{
use TraitA::a insteadof TraitB::a;
}
}
EDIT: Oh hang on, you're talking about class versus mixin, not mixin versus mixin.
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Wed Jan 26, 2011 9:26 am
by kikito
leiradel wrote:What happens if you have method "a" in a class and the class includes a mixin which has also an "a" method when using middleclass? Which one will instances use?
On middleclass the rule is: the last method included on the class (via includes or regular definitions) always wins.
Code: Select all
Cow = class('Cow')
function Cow:talk() print('original method') end
local lily = Cow:new()
lily:talk() -- original method
Mixin = { function talk() print('mixin method') end }
Cow:include(Mixin) -- this will copy Mixin.talk over Cow.talk
lily:talk() -- mixin method
-- this will not change anything, since Mixin.talk was copied, not just 'referenced' on Cow
Mixin.talk = function() print('modified mixin method') end
lily:talk() -- mixin method
-- this will copy a new talk implementation on Cow
function Cow:talk() print('overriden mixin method') end
lily:talk() -- overriden mixin method
Notice that this particular example would work for class methods as well (replacing lily:talk() by Cow:talk())
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Tue Feb 01, 2011 8:31 am
by kikito
Middleclass 1.3 is out!
This is a backwards-incompatible update.
The only change is that I've removed
super.
On middleclass 1.2, you could do this:
Code: Select all
MySubClass = class('MySubClass', MySuperClass)
function MySubClass:initialize()
super.initialize(self, ...)
end
On 1.3, you can't invoke 'super' any more. You have to explicitly use 'MySuperClass' instead, like this:
Code: Select all
MySubClass = class('MySubClass', MySuperClass)
function MySubClass:initialize()
MySuperClass.initialize(self, ...)
end
This change was done for performance and elegance reasons; super was adding too much overhead to middleclass, both in terms of efficiency and maintenance effort. The benefits it gave were just not worth the trouble. Feedback from the community helped me figure that one out.
I've also updated middleclass-extras (now 0.9) to account for this change.
Regards!
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Tue Feb 01, 2011 9:16 am
by BlackBulletIV
Awesome! I better get to work and convert Grace to be compatible.
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Sun Feb 06, 2011 9:04 pm
by TechnoCat
I can't get Class:implements(Stateful) to work anymore.
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Sun Feb 06, 2011 9:14 pm
by BlackBulletIV
Aren't you meant to use Class:include(Stateful)?
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Sun Feb 06, 2011 9:14 pm
by slime
I think the syntax is now
EDIT: Ninja'd.
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Sun Feb 06, 2011 9:16 pm
by leiradel
kikito wrote:Middleclass 1.3 is out!
This is a backwards-incompatible update.
Sweet, thanks!
Shouldn't you bump the version up to 2.0 since it's a backwards-incompatible release?
Cheers,
Andre
Re: middleclass & middleclass-extras: OOP for LUA
Posted: Sun Feb 06, 2011 9:18 pm
by TechnoCat
Yeah, :include is right.
I was looking at the doc in the head of the Stateful file
https://github.com/kikito/middleclass-e ... ateful.lua