An attempt at unifying lua class libraries to provide a common API.
Latest commit 655f8fb Jul 25, 2015 @bartbes Merge pull request #8 from tst2005/patch-1
typo fix
Failed to load latest commit information.
tests @ 753dce1
.gitmodules Use the separate tests repo as a submodule Oct 25, 2011 typo fix Jul 25, 2015 Add specification for libraries using class commons, and specifically… Jul 21, 2013

Class Commons

Class Commons is a project to provide a common compatibility interface for class systems. The goal of this is to make libraries independent of class libraries, a library using the Class Commons API can then be used with any class system adhering to it (albeit via a compatibility layer).



  • Single-class inheritance
  • Constructors
  • Instance methods
  • Polymorphism

Class definition is single-write read-only, so the entire class has to be defined on creation.


class = common.class(name, table, parents...)
instance = common.instance(class, ...)

Class constructors

Constructors are defined by the special init function:

foo = {}
function foo:init() = "baz"
foo = common.class("foo", foo)

Derived classes may access the super class constructors by using <super>.init, but ONLY if the parent was created using common.class:

foo = common.class("foo", {init = function(self) = true end})
bar = common.class("bar", {init = function(self) foo.init(self) end})

NOTE: Accessing <super>.init from classes not created with common.class yields undefined behaviour.


Instances may be created using common.instance:

foo = common.class("foo", {init = function(self, bar) = bar end})
baz = common.instance(foo, 'baz')


local Tree = {}
function Tree:grow()
    print("Is a big tree now!")

function Tree:chop()
    print("Chopped down a tree.")

local Ent = {}
function Ent:chop()
    print("I am no tree, I am an ent!")

Tree = common.class("Tree", Tree)
Ent = common.class("Ent", Ent, Tree)

local tree = common.instance(Ent)
tree:grow() --> Is a big tree now!
tree:chop() --> I am no tree, I am an ent!

Participating libraries

Repository information

This repository will both contain documentation (like this very document) and tests. (Note: located in a subrepository.) The authors of participating libraries all get write access, and are free, and encouraged, to collaborate.