Page 1 of 1

How do you handle classes?

Posted: Mon May 09, 2016 11:18 pm
by Vimm
I've noticed that people seem to do classes differently, which I find weird cuz I never knew it could be done in different ways XD

I learned using classic.lua but apparently it isnt a great way to do it, so I wanna see how other people go about it!

lately ive been adding

Code: Select all

module(..., package.seeall)
to the top of my classes, which is something I saw in a youtube video, how do you guys do it?

Re: How do you handle classes?

Posted: Mon May 09, 2016 11:41 pm
by Beelz
Lua doesn't have a set in stone version of how classes should be... That's the beauty of it. If you find a way to do it that suits your needs and you are comfortable with, then by all means I would say to roll with it. I like to do things myself and keep it simple, so I prefer not to use libraries like classic, 30log, or middleclass. This is the entirety of my class.lua that I drop into my projects:

Code: Select all

local class = {}

function class:extend(subClass)
	return setmetatable(subClass or {}, {__index = self})
end

function class:new(...)
	local copy = setmetatable({}, {__index = self})
	return copy, copy:init(...)
end

function class:init(...) end

return class

Re: How do you handle classes?

Posted: Tue May 10, 2016 12:26 am
by Inny
module(..., package.seeall) is discouraged. There's a huge and lengthy reason for it, but you don't need to know why right now unless you want (in which case you can start here: http://lua-users.org/wiki/LuaModuleFunctionCritiqued ). Also, the module function doesn't make classes, it makes modules.

As for classes, I highly recommend you use either kikito's middleclass or Yonaba's 30log. If you want to go your own way that's fine, but if you use these, then you can get a lot of help from people on this board (even from the library writers themselves). You own homebrewed class library would leave you to fend for yourself if you ran into problems.

Re: How do you handle classes?

Posted: Tue May 10, 2016 12:31 am
by Vimm
Inny wrote:module(..., package.seeall) is discouraged. There's a huge and lengthy reason for it, but you don't need to know why right now unless you want (in which case you can start here: http://lua-users.org/wiki/LuaModuleFunctionCritiqued ). Also, the module function doesn't make classes, it makes modules.

As for classes, I highly recommend you use either kikito's middleclass or Yonaba's 30log. If you want to go your own way that's fine, but if you use these, then you can get a lot of help from people on this board (even from the library writers themselves). You own homebrewed class library would leave you to fend for yourself if you ran into problems.
Ohh I like Kikito's stuff, ill probably look at middleclass! I dont think I understand enough about lua to make my own class.lua lol

Re: How do you handle classes?

Posted: Wed May 11, 2016 6:08 pm
by airstruck
You might also consider using factory functions instead of classes if you don't need static class members. In other words, just write a function that creates, initializes, and returns a table, similar to what your class constructor would do (except create the table and return it instead of just initializing it). Inheritance can be achieved by writing factories that serve as decorators for tables created by other factories. This should produce very straightforward and understandable code, and won't require any special knowledge of Lua metatable stuff or any dependencies.