middleclass & extras: middleclass 3.0 is out!

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: middleclass & extras: middleclass 3.0 is out!

Post by Roland_Yonaba »

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". :megagrin:
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & extras: middleclass 3.0 is out!

Post by kikito »

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 don't think libraries should never be called with load. That's what require is for. Leave lf.load for things like loading levels.
@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.
User avatar
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!

Post by BlackBulletIV »

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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & extras: middleclass 3.0 is out!

Post by kikito »

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.
User avatar
SiENcE
Party member
Posts: 805
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: middleclass & extras: middleclass 3.0 is out!

Post by SiENcE »

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
		print('I am an adult.')
	end
end

function love.load()
	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
User avatar
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!

Post by Robin »

Add this to the end of person.lua:

Code: Select all

return Person
Help us help you: attach a .love.
User avatar
SiENcE
Party member
Posts: 805
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: middleclass & extras: middleclass 3.0 is out!

Post by SiENcE »

Uh of couse ... where can i hide myself :cool:

Thanks!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: middleclass & extras: middleclass 3.0 is out!

Post by kikito »

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.
User avatar
murks
Party member
Posts: 185
Joined: Tue Jun 03, 2014 4:18 pm

Re: middleclass & extras: middleclass 3.0 is out!

Post by murks »

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?
User avatar
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!

Post by bartbes »

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

Who is online

Users browsing this forum: Bing [Bot] and 1 guest