Log In  

I decided to make a simple class implementation. Here's what I managed to come up with:

function class(c)
    name=c[1]
    _𝘦𝘯𝘷[name]=function(...)
        local initargs={...}
        local obj={___fnccore={}}
        for k,f in pairs(c)do
            if type(f)=='function'then
                obj.___fnccore[k]=f
                obj[k]=function(...)
                    obj.___fnccore[k](obj,...)
                end
            end
        end
        obj.__init__(unpack(initargs))
        return obj
    end
end

The (preferred) syntax to use this with looks like this:

class { 'classname'
    __init__=function(self,args)
        -- Stuff goes here --
    end,
    -- Other functions here with "self" or something like that as the first argument --
}

Here's an example:

class { 'printer',
    __init__=function(self,a,b)
        self.a=a
        self.b=b
    end,
    thing=function(self,c)
        print(self.a..self.b..c)
    end
}
test=printer('one ','two')
test.thing(' three')

The output for the above would be:

one two three

Here is the cartridge containing the above example:

Cart #graffiticlasses-0 | 2021-06-01 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Feedback would be nice!

P#92893 2021-06-01 16:32

have you looked at metatable?
see: https://www.lua.org/pil/16.html

P#92899 2021-06-01 19:58

I know what metatables are, yes. What's your point?

P#92910 2021-06-02 05:15

actually your framework does something native to lua, eg passing self to a function call.
replace:

test.thing(3)

by:

test:thing(3)

and ‘self’ will automatically be the first parameter

P#92912 2021-06-02 06:01

The reason I picked my style of 'self', is because my main programming language is Python and I wanted it to feel like that.

P#92913 2021-06-02 06:31

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2022-11-27 08:10:55 | 0.010s | Q:20