middleclass & extras: middleclass 3.0 is out!

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by TechnoCat »

kikito wrote:I'm browsing the site from my mobile and won't be able to give this a look until tomorrow evening.

Is have never used Lube.

Is that how it is supposed to work, redefining the love callbacks, org is that your own invention?
LUBE is not being used yet, I am just calling it and setting it up to be used. Overriding the callbacks was my idea.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by kikito »

Ok I had a quick look this morning and it seems easy enough.

StatefulObject is a superclass. As all superclasses, it needs to perform some initializations on your objects before it can be properly used. In other words, you need this on Game.lua:

Code: Select all

Game = class("Game", StatefulObject)
function Game:initialize()
  super.initialize(self)  --> this line
end
Alternatively, you could have removed the function Game:initialize() completely, and it would also have worked (the default implementation calls super.initialize(self) 'upwards on the hierarchy')

I'll make sure I put some guards on the StatefulObject methods so a proper assert gets fired (along the lines of 'You should call blahblahblah on your initializer'). Or maybe I'll transform StatefulObject into a Mixin. Mmmm...

On other order of things, I think you overcomplicated the code a bit. The "NetTest" object isn't really necessary; you could use a "Game" object. "Server" would be just one state of game. The game object could be referenced as 'self' inside all methods. Have you seen the Game controller example I put on the wiki?
When I write def I mean function.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by zac352 »

I wrote a class file that initiated a class type like this:

Code: Select all

class "type" {
property=value;
__method=function;
...
}
Hello, I am not dead.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by kikito »

I thought about doing something like that. But the syntax kept getting in the way.
  • Doesn't scale too well for using subclassing, because additional parameters aren't easily included. Implementing something that does class "MyClass"(MySuperClass){...} would still be possible, but I didn't like it. I had to create 3 objects with metatables just to accomodate one syntax.
  • Classes declared that way are forced to be global (or local). I prefer letting the user decide the scope of their classes by assigning them to a variable.
  • Functions enclosed on the { ... }, can not use lua's the implicit 'self' parameter; they have to be explicit about it: f = function(self, ...). I find the implicit version more elegant, and I encourage its usage whenever possible (with one exception: class methods should have it explicit so it can be renamed to theClass)
When I write def I mean function.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by zac352 »

I'm thinking of rewriting it to accept:

Code: Select all

class "blah" (superclass1, superclass2, ...) {
...
}
All you do is return a function that returns a function, all developing a class as they are called.
Hello, I am not dead.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by kikito »

zac352 wrote:All you do is return a function that returns a function, all developing a class as they are called.
Is not as simple. Consider the following code:

Code: Select all

class "blah" (superclass1, superclass2, ...) 
It is sometimes useful to create classes without adding additional methods to them. Yeah, you could bypass this by adding an empty table {} at the end, but that would kindof defeat the point of having a pretty syntax on the first place.

And there are 2 variants:

Code: Select all

class "blah" {...} 

Code: Select all

class "blah" 
You would have to return not functions, but tables - with the __call metamethod set to something special. That's what I meant when I said "I have to create 3 objects just to accommodate one syntax".

When I was considering the possibilities, I estimated that coping with so much tables & metatables would add ~100 lines of code to MiddleClass. Which is 124 lines right now. So I didn't follow that path. If you continue with this, please let me know if I was wrong.
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by TechnoCat »

kikito wrote:StatefulObject is a superclass. As all superclasses, it needs to perform some initializations on your objects before it can be properly used. In other words, you need this on Game.lua:

Code: Select all

Game = class("Game", StatefulObject)
function Game:initialize()
  super.initialize(self)  --> this line
end
This was indeed my problem. Thanks.
kikito wrote:On other order of things, I think you overcomplicated the code a bit. The "NetTest" object isn't really necessary; you could use a "Game" object. "Server" would be just one state of game. The game object could be referenced as 'self' inside all methods. Have you seen the Game controller example I put on the wiki?
I have seen the example, that is how I got started. And I kind of see why you are trying to simplify it, but I'm not sure simplifying it would be beneficial. I'll think about alternative implementations some more later today. But any more questions I have related to the implementation of my love program would probably be better in a separate thread.

The verdict is: I love middleclass and middleclass-extras.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by kikito »

But any more questions I have related to the implementation of my love program would probably be better in a separate thread.
As you wish. This thread isn't particularly crowded anyway.
The verdict is: I love middleclass and middleclass-extras.

Code: Select all

self.ego = self.ego + 30
:ultraglee:
When I write def I mean function.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by zac352 »

Thanks kikito, you made my 30 lines of object orientation sound like physics. :cry:
Hello, I am not dead.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & middleclass-extras: Object Orientation for

Post by kikito »

zac352 wrote:Thanks kikito, you made my 30 lines of object orientation sound like physics. :cry:
There, there.

I just have already been on the "I'll make syntactic sugar for my class system on lua" place. I could not find a way to do it that was elegant, short and efficient; had to trade at least one of those. See this stackoverflow question, for example. That was elegant and short enough, but not very efficient. So I didn't include it on middleclass.

For now, I'm on the opinion that in Lua too much syntactic sugar can make your code "too fat".

But I'd be glad to be shown better. If you think you have found a better compromise between syntax, elegance, efficiency and length, please do let me know.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 75 guests