Opinions on my class library?

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Jipjang
Prole
Posts: 6
Joined: Fri May 24, 2024 7:04 am

Opinions on my class library?

Post by Jipjang »

I made a very simple and minimalistic class library.

Code: Select all

local baseclass

local function newclass(parent, ...)
    local t = setmetatable( {
        __call = function(t, ...)
            return newclass(t, ...)
        end
    }, parent )
    t.__index = t

    local init = t.init
    if init then init(t, ...) end

    return t
end

baseclass = newclass
{
    __call = function(t, ...)
        return newclass(t, ...)
    end
}

return baseclass
Here's a classic animal+dog+sheep example.

Code: Select all

local animal = class()

function animal:init(sound)
    self.sound = sound
end

function animal:makesound()
    print(self.sound)
end

function animal:walk()
    print("Step, step, step...")
end

local dog = animal()

function dog:makesound()
    print("BARK! "..self.sound)
end

function dog:jump()
    print("I see my house from up here!")
end

local maddog = dog()

function maddog:makesound()
    print("RAAH! BARK! "..self.sound)
end

function maddog:explode()
    print("BOOM!")
end

local inst1 = animal("Hi!")
inst1:makesound() -- Hi!
-- inst1:jump() -- Crash: undefined.

local inst2 = dog("Bye!")
inst2:makesound() -- BARK! Bye!
inst2:jump() -- I see my house from up here!

local inst3 = maddog("Hey!")
inst3:makesound() -- RAAH! BARK! Bye!
inst3:explode() -- BOOM!
inst3:jump() -- I see my house from up here!

local inst4 = animal("Hello!")
inst4:makesound() -- Hello!
inst1:makesound() -- Hi!
Static fields:

Code: Select all

local c = class()
c.staticfield = 1
function c:updatestatic()
	c.staticfield = 2
end
User avatar
ivan
Party member
Posts: 1939
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Opinions on my class library?

Post by ivan »

How would you call a function defined in the parent class?
RNavega
Party member
Posts: 456
Joined: Sun Aug 16, 2020 1:28 pm

Re: Opinions on my class library?

Post by RNavega »

That __call feature is great, as it mimics the instance creation syntax from things like Python. As you show in your example:

Code: Select all

local dog = animal(param1, param2, param3, ...)
However, I recently changed my mind about using metatables to mimic classes.
Every inherited function will end up being a key miss -- that is, you call "myObject.myFunction()", the "myFunction" key is tested on "myObject" and fails, then LuaJIT sees if there's a metatable in that object with the __index field, then it repeats the process by calling "myFunction" from __index and so on.

For a speed junkie, it feels more fulfilling to use a factory function that fills up the instance table with all the methods, variables etc that it should have, becoming local keys to that table. It's an annoying extra effort that gets you the absolute maximum performance.

So using your animal base class as an example, but using the factory function way:

Code: Select all

function animal_makesound(self)
    print(self.sound)
end

function animal_walk(self)
    print("Step, step, step...")
end

function animal_create(sound)
    local t = {
        -- Variables, state etc.
        sound = sound,

        -- The "contract" / interface.
        makesound = animal_makesound,
        walk      = animal_walk
    }
    -- Other initialization.
    -- (...)
    -- Return the new instance.
    return t
end
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests