middleclass & extras: middleclass 3.0 is out!

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: middleclass & middleclass-extras: OOP for LUA

Post 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
User avatar
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

Post 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.
Last edited by BlackBulletIV on Wed Jan 26, 2011 10:01 am, edited 1 time in total.
User avatar
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

Post 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())
When I write def I mean function.
User avatar
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

Post 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!
Last edited by kikito on Tue Feb 01, 2011 10:14 am, edited 1 time in total.
When I write def I mean function.
User avatar
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

Post by BlackBulletIV »

Awesome! I better get to work and convert Grace to be compatible.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by TechnoCat »

I can't get Class:implements(Stateful) to work anymore.
User avatar
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

Post by BlackBulletIV »

Aren't you meant to use Class:include(Stateful)?
User avatar
slime
Solid Snayke
Posts: 3160
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post by slime »

I think the syntax is now

Code: Select all

MyClass:include(Stateful)
EDIT: Ninja'd. :(
User avatar
leiradel
Party member
Posts: 184
Joined: Thu Mar 11, 2010 3:40 am
Location: Lisbon, Portugal

Re: middleclass & middleclass-extras: OOP for LUA

Post 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
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: middleclass & middleclass-extras: OOP for LUA

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests