## middleclass & extras: middleclass 3.0 is out!

Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

### Re: middleclass & extras: middleclass 3.0 is out!

Nixola wrote:I fear the path trick doesn't work if someone uses lf.load instead of require. There should be no reason for that, but you can never be too sure
I can confirm that it won't work. When a file is called with require, it passes as arg the path to the module. The trick Kikito mentions captures that path, and removes the module name at the end, so that it just remains the top folder address.
I tend to use the same in my libs, games. I think it is nicer than those dofile/loadfile.

But since MiddleClass is a library, IMO it should not be called using load.filesystem.load, or loadfile, or dofile. require should preferred here.

@kikito: I do recommended to update the Readme and add some lines / snippets for that path trick.
kikito wrote:middleclass was my learning project in Lua.
Quite impressive for a learning project. Mine was a "hello world".

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: middleclass & extras: middleclass 3.0 is out!

Nixola wrote:I fear the path trick doesn't work if someone uses lf.load instead of require. There should be no reason for that, but you can never be too sure
@kikito: I do recommended to update the Readme and add some lines / snippets for that path trick.
It's not needed in middleclass, since it is a single file. I might do a blog post about my thoughts of Lua libraries, though.
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 & extras: middleclass 3.0 is out!

kikito wrote:

Code: Select all

assert(MyClass:new():isInstanceOf(Object))
And if a table is not a class, that code will throw an error, unlike in Middleclass 2.0 where you could pass any value to instanceOf and it would return false instead. I think the ease-of-use and flexibility afforded by global variables makes them worth it in many cases. If someone hates the name of something, they can easily change it.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: middleclass & extras: middleclass 3.0 is out!

BlackBulletIV wrote:
kikito wrote:

Code: Select all

assert(MyClass:new():isInstanceOf(Object))
And if a table is not a class, that code will throw an error, unlike in Middleclass 2.0 where you could pass any value to instanceOf and it would return false instead. I think the ease-of-use and flexibility afforded by global variables makes them worth it in many cases. If someone hates the name of something, they can easily change it.
You have inquisitive eyes. That's good. But in this case you overlooked something. It's explained at the end of the update guide. If you are not sure that obj is an instance, you can use Object.isInstanceOf like this:

Code: Select all

assert(Object.isInstanceOf(obj, Object))
That will throw no errors, even if obj is something like nil or a number. So this version is semantically equivalent to the previous one, but it is also a bit more convenient if you know obj is an object. You can also personalize it for each class, which could be useful in some cases.
When I write def I mean function.

SiENcE
Party member
Posts: 785
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

### Re: middleclass & extras: middleclass 3.0 is out!

A question came up while using middleclass. I thought it must be possible, but i get an error.

I use your wiki sample to explain.

Two files, two classes. I don't want the Person class to be global.
When I try to make a AgedPerson class and use Person as subclass I get this: "Middleclass.lua:174: attempt to index local 'super' (a boolean value). Where is my problem?

main.lua

Code: Select all

local class = require('lib/MiddleClass')
local Person = require('person')

AgedPerson = class('AgedPerson', Person) -- or Person:subclass('AgedPerson')
AgedPerson.static.ADULT_AGE = 18 --this is a class variable
function AgedPerson:initialize(name, age)
Person.initialize(self, name) -- this calls the parent's constructor (Person.initialize) on self
self.age = age
end
function AgedPerson:speak()
Person.speak(self) -- prints "Hi, I am xx."
if(self.age < AgedPerson.ADULT_AGE) then --accessing a class variable from an instance method
print('I am underaged.')
else
end
end

local p1 = AgedPerson:new('Billy the Kid', 13) -- this is equivalent to AgedPerson('Billy the Kid', 13) - the :new part is implicit
local p2 = AgedPerson:new('Luke Skywalker', 21)
p1:speak()
p2:speak()
end
person.lua

Code: Select all

local class = require('lib/MiddleClass')

local Person = class('Person') --this is the same as class('Person', Object) or Object:subclass('Person')
function Person:initialize(name)
self.name = name
end
function Person:getName()
print(self)
return self.name
end
function Person:speak()
print('Hi, I am ' .. self:getName() ..'.')
end

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: middleclass & extras: middleclass 3.0 is out!

Add this to the end of person.lua:

Code: Select all

return Person

SiENcE
Party member
Posts: 785
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

### Re: middleclass & extras: middleclass 3.0 is out!

Uh of couse ... where can i hide myself

Thanks!

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: middleclass & extras: middleclass 3.0 is out!

Also, remember that while this works in LÖVE:

Code: Select all

local class = require('lib/MiddleClass')
It is not guaranteed to work in the future. Your code will be more future-proof if you use dots instead:

Code: Select all

local class = require('lib.MiddleClass')
When I write def I mean function.

murks
Party member
Posts: 185
Joined: Tue Jun 03, 2014 4:18 pm

### Re: middleclass & extras: middleclass 3.0 is out!

Hi there,
I consider using middleclass and looked through the implementation. One thing in there strikes me as odd, line 128 in the current implementation:

Code: Select all

function Object.static:subclassed(other) end
This does effectively nothing.

It is used in

Code: Select all

function Object.static:subclass(name)
but it still does effectively nothing. What is the purpose of this?

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: middleclass & extras: middleclass 3.0 is out!

I assume it's there so you can override it.

### Who is online

Users browsing this forum: No registered users and 15 guests