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
middleclass & extras: middleclass 3.0 is out!
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: middleclass & middleclass-extras: OOP for LUA
I know in PHP 5.4 (they call mixins traits) handles that situation by allowing the user to choose:
EDIT: Oh hang on, you're talking about class versus mixin, not mixin versus mixin.
Code: Select all
class Foo
{
use TraitA, TraitB
{
use TraitA::a insteadof TraitB::a;
}
}
Last edited by BlackBulletIV on Wed Jan 26, 2011 10:01 am, edited 1 time in total.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: middleclass & middleclass-extras: OOP for LUA
On middleclass the rule is: the last method included on the class (via includes or regular definitions) always wins.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?
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
When I write def I mean function.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: middleclass & middleclass-extras: OOP for LUA
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:
On 1.3, you can't invoke 'super' any more. You have to explicitly use 'MySuperClass' instead, like this:
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!
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
Code: Select all
MySubClass = class('MySubClass', MySuperClass)
function MySubClass:initialize()
MySuperClass.initialize(self, ...)
end
I've also updated middleclass-extras (now 0.9) to account for this change.
Regards!
Last edited by kikito on Tue Feb 01, 2011 10:14 am, edited 1 time in total.
When I write def I mean function.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: middleclass & middleclass-extras: OOP for LUA
Awesome! I better get to work and convert Grace to be compatible.
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: middleclass & middleclass-extras: OOP for LUA
I can't get Class:implements(Stateful) to work anymore.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: middleclass & middleclass-extras: OOP for LUA
Aren't you meant to use Class:include(Stateful)?
- slime
- Solid Snayke
- Posts: 3170
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: middleclass & middleclass-extras: OOP for LUA
I think the syntax is now
EDIT: Ninja'd.
Code: Select all
MyClass:include(Stateful)
Re: middleclass & middleclass-extras: OOP for LUA
Sweet, thanks!kikito wrote:Middleclass 1.3 is out!
This is a backwards-incompatible update.
Shouldn't you bump the version up to 2.0 since it's a backwards-incompatible release?
Cheers,
Andre
- TechnoCat
- Inner party member
- Posts: 1612
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: middleclass & middleclass-extras: OOP for LUA
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
I was looking at the doc in the head of the Stateful file https://github.com/kikito/middleclass-e ... ateful.lua
Who is online
Users browsing this forum: Google [Bot] and 2 guests