Page 1 of 1

Looking for fast and simple OOP library

Posted: Sun Jan 22, 2017 10:58 pm
by reno57
Hello,

For my project i use an OOP library : GitHub - rxi/classic: Tiny class module for Lua.
I realized that there is others libraries like : GitHub - kikito/middleclass : A simple OOP library for Lua.

As i'm looking for a fast and easy to use OOP library, do you think my choice is right ? or, may be i should switch to kikito's library or to another one ?

Have you some library to suggest ?

thanks in advance

Re: Looking for fast and simple OOP library

Posted: Sun Jan 22, 2017 11:33 pm
by Positive07
I generaly use Middleclass, but you should use whatever you feel better with. If you like Classic then just go with it.

I think currently the only one that takes performance as the first objective is Classy which is similar to Middleclass. And the one that takes filesize into consideration is 30log, Classic is also really tiny!

Middleclass is the most complete one, with inheritance, full metamethods support and subclassing. Also I consider it's documentation to be great!

Again, use whatever you think works best, you should try them and decide on yourself. They are all very similar though!

Re: Looking for fast and simple OOP library

Posted: Mon Jan 30, 2017 8:22 pm
by reno57
Thanks Positive07, to share your experience about OOP library.

Re: Looking for fast and simple OOP library

Posted: Tue Jan 31, 2017 3:38 am
by scissors61
I think the forum was trying to answer this question, and I agree.

Re: Looking for fast and simple OOP library

Posted: Tue Jan 31, 2017 5:56 am
by airstruck
As i'm looking for a fast and easy to use OOP library, do you think my choice is right?
Is it fast and easy to use? Does it have the features you need, but not too many features you don't need? If so, it's probably fine.

If you only use single inheritance, and don't need mixins or other fancy stuff, it should be either fairly trivial or somewhat educational to whip something up yourself; for example if you just want the convenience of being able to define methods with the Class:method syntax. Here's the "class.lua" from a project I worked on recently where I decided to use classes to achieve OOP.

Code: Select all

return function (super)
    local meta = { __index = {} }
    return setmetatable(meta.__index, {
        __call = function (_, ...)
            local instance = setmetatable({}, meta)
            if instance.init then
                instance:init(...)
            end
            return instance
        end,
        __index = super
    })
end
Also probably worth noting, the "class" metaphor is really not necessary for OOP in Lua. You can just as well use factory functions:

Code: Select all

local bark = function (self) print(self.name .. ' says woof') end
local Dog = function (name) return { name = name, bark = bark } end

local dog = Dog('Rufus')
dog:bark() -- 'Rufus says woof'