Advanced object oriented module for Lua (OOP)
Lua Python
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
dmc_lua update module libs May 3, 2015
spec add names to Classes Feb 19, 2015
LICENSE Update Jan 11, 2015
Snakefile update for new hierarchy Apr 28, 2015


Advanced object oriented module for Lua (OOP)

This single-file module started its life as dmc-objects and was used to create mobile apps built with the Corona SDK. It was later refactored into two files lua_objects.lua & dmc_objects.lua so that pure-Lua environments could benefit, too (eg, lua-corovel).

This power-duo have been used to create relatively complex Lua mobile apps (~60k LOC), clients for websockets and the WAMP-protocol, and countless others.


  • new! customizable methods and names for constructor/destructor
  • new! multiple inheritance (all way to top level)
  • new! handles ambiguities of inherited attributes
  • new! advanced support for mixins
  • getters and setters
  • correctly handles missing methods on super classes
  • optimization (copy methods from super classes)
  • new! unit tested


A Simple Custom Class

Here's a quick example showing how to create a custom class.

--== Import module

local Objects = require 'dmc_lua.lua_objects'

--== Create a class

local AccountClass = newClass()

--== Class Properties

AccountClass.DEFAULT_PATH = '/path/dir/'
AccountClass.DEFAULT_AMOUNT = 100.45

--== Class constructor/destructor

-- called from obj:new()
function AccountClass:__new__( params )
	params = params or {}
	self._secure = or true 
	self._amount = params.amount or self.DEFAULT_AMOUNT 

-- called from obj:destroy()
function AccountClass:__destroy__()
	self._secure = nil 
	self._amount = nil 

--== Class getters/setters

function AccountClass.__setters:secure( value )
	assert( type(value)=='boolean', "property 'secure' must be boolean" )
	self._secure = value
function AccountClass.__getters:secure()
	return self._secure

--== Class methods

function AccountClass:deposit( amount )
	self._amount = self._amount + amount
	self:dispatchEvent( AccountClass.AMOUNT_CHANGED_EVENT, { amount=self._amount } )
function AccountClass:withdraw( amount )
	self._amount = self._amount - amount

Create Class Instance

And here's how to work with that class.

-- Create instance

local account = AccountClass:new{ secure=true, amount=94.32 }

-- Call methods

account:deposit( 32.12 )
account:withdraw( 50.00 )

-- optimize method lookup


-- Check class/object types 

assert( AccountClass.is_class == true ), "AccountClass is a class" )
assert( AccountClass.is_instance == false ), "AccountClass is not an instance" )

assert( obj.is_class == false, "an object instance is not a class" ) 
assert( obj.is_instance == true, "an objects is an instance of a class" )
assert( obj:isa( AccountClass ) == true, "this obj is an instance of AccountClass" )

-- Destroy instance

account = nil 

More, Advanced Examples

The project dmc-objects contains two lua-objects sub-classes made for mobile development (ObjectBase & ComponentBase). These sub-classes show how to get more out of lua_objects, such as:

  • custom initialization and teardown
  • custom constructor/destructor names
  • custom Event mixin (add/removeListener/dispatchEvent) lua-events-mixin

Custom Constructor/Destructor

You can even customize the names used for construction and destruction.

-- use 'create' instead of 'new'
-- eg,  MyClass:create{ secure=true, amount=94.32 }
registerCtorName( 'create' )

-- use 'removeSelf' instead of 'destroy'
-- eg,  obj:removeSelf()
registerDtorName( 'removeSelf' )