lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Aug 30, 2007, at 11:25, Michal Kolodziejczyk wrote:

I woul like to write some classes (to get some OO and inheritance),
which could also be modules (to get autoload and 'dotted' names
notation). What is the way to accomplish it?

Tomas demonstrated something a while back which elegantly leverage Lua's built-in module system:

Something along these lines:

local function extends( aSuper )
    return function( aClass )
        setmetatable( aClass, aSuper )
        aClass.__index = aClass

        aClass.self = aClass
        aClass.super = aSuper

        return aClass

local class = module

class( 'Object', extends(), package.seeall )

class( 'MyObject', extends( Object ) )