Skip to main content

Class Factory

The module returns a callable table.

local Class = require("class")

Class(definition)

Creates a class from a definition table.

local Example = Class({
init = function(self)
self.ready = true
end,
})

Equivalent to Class:new(definition).

Class:new(definition)

Creates a class and prepares its metatable.

Definition fields

FieldDescription
initOptional constructor called for each instance.
__typeOptional readable type name. Defaults to "Class".
__includesOptional table, class, string, or list of includes.
methodsAny function fields become instance methods.

Class:registerClass(name, prototype, parent)

Creates a typed class from a prototype and optional parent.

local Registered = Class:registerClass("Registered", {
hello = function()
return "hello"
end,
})

The returned class has __type set to name and operator helpers enabled.