Introduction

A lightweight, flexible, and extensible object-oriented programming (OOP) system for Lua.
It provides class creation, inheritance, encapsulation, operator overloading, and utility helpers, all in a single file.
Designed for seamless integration into any Lua project, with support for private members and advanced debugging tools.


Installation


local Class	=	require("class")
						

init

Initialization function automatically called when creating an object via 'Class()'.
Defined inside the table passed to the constructor.

  • Arguments :
  •     [1] : tSelf — The instance being initialized
  •     [2] : ... — Additional arguments passed during instantiation
  • Returns :
  •     [1] : nil
  • Exceptions :
  •     [1] : Throws an error wrapped via xpcall if init fails

local A		=	Class{
	init	=	function(self)
		print("Object initialized!")
	end,
}
local obj	=	A() -- Prints "Object initialized!"
						

include

Recursively merges two classes (or tables) by copying their members.
It respects metatables.

  • Arguments :
  •     [1] : tBase — The base class or table to include into
  •     [2] : tSource — The class or table to merge from
  • Returns :
  •     [1] : tBase — The merged class
  • Exceptions :
  •     [1] : Throws if tBase or tSource are not tables

local Mixin	=	{
	say	=	function() print("Hello") end
}

local A		=	Class{}
A			=	Class:include(A, Mixin)
A.say() -- Hello
						

clone

Creates a deep copy of a class or instance, preserving its metatable.

  • Arguments :
  •     [1] : tObject — The class or instance to clone
  • Returns :
  •     [1] : tCloned — A deep copy of the input
  • Exceptions :
  •     [1] : Throws if the object cannot be cloned

local A	=	Class{
	foo	=	"bar"
}

local B	=	Class:clone(A)
print(B.foo) -- bar
						

DebugInfos

Prints the internal details of an instance: type, private members, methods.

  • Arguments :
  •     [1] : tSelf — The instance whose internals will be printed
  • Returns :
  •     [1] : string — Formatted debug information
  • Exceptions :
  •     [1] : None

local A		=	Class{
	foo	=	function() end
}
local obj	=	A()
print(obj:DebugInfos())
						

accessor

Generates getter and setter functions for a private member.

  • Arguments :
  •     [1] : tSelf — The instance to add accessors to
  •     [2] : sInternal — Name of the private key
  •     [3] : sExternal — Public-facing name (used in getter/setter)
  •     [4] : vDefault — (Optional) Default value to assign
  • Returns :
  •     [1] : nil
  • Exceptions :
  •     [1] : Throws if parameters are missing or invalid

local A		=	Class{
	init	=	function(self)
		Class:accessor(self, "sName", "Name", "John")
	end
}
local a		=	A()
a:SetName("Doe")
print(a:GetName()) -- Doe
						

overloadOperators

Overloads the operator to merge two instances of the same type.

  • Arguments :
  •     [1] : tClass — The class to extend with operator overloading
  • Returns :
  •     [1] : nil
  • Exceptions :
  •     [1] : Throws if tClass is not a valid class

local A	=	Class{
	init	=	function(self)
		self.__private.value	=	42
	end
}

Class:overloadOperators(A)

local a1		=	A()
local a2		=	A()
local result	=	a1 + a2
						

new

Creates a new class, supports options like multiple inheritance via '__includes'.

  • Arguments :
  •     [1] : tDefinition — Table defining the class (methods, __includes, etc.)
  • Returns :
  •     [1] : tClass — A new class instance
  • Exceptions :
  •     [1] : Throws if definition is invalid or causes a conflict

local MyClass	=	Class:new{
	init	=	function(self)
		print("Hello world")
	end
}

local obj	=	MyClass()
						

registerClass

Registers a class with a name, prototype, and optional parent class.
Useful for centralized registries.

  • Arguments :
  •     [1] : sName — The class type name
  •     [2] : tPrototype — Table containing methods/fields
  •     [3] : tParent — (Optional) Class to inherit from
  • Returns :
  •     [1] : tClass — Registered class with name and inheritance
  • Exceptions :
  •     [1] : Throws if sName is not a string or prototype is invalid

local A	=	{	foo	=	function() end	}
local B	=	{	bar	=	function() end	}

local C	=	Class:registerClass("MyType", A, B)
print(C.__type) -- MyType
						

__includes

Inheritance helper that allows a class to include methods and properties from other classes or tables.


local A	=	Class{
    foo	=	function() print('foo') end
}

-- single inheritance
B = Class{__includes = A}
instance = B()
instance:foo() -- prints 'foo'
instance:bar() -- error: function not defined
					

__type

Defines the internal type string of an object, useful for type checking and debugging.


local MyClass = {}
MyClass.__type = "MyClass"

local obj = setmetatable({}, { __index = MyClass })
print(obj.__type) -- Output: MyClass
					

__gc

Called by Lua garbage collector to perform cleanup when an object is destroyed.


local MyClass = {}

local mt = {
	__gc = function(obj)
		print("Object is being garbage collected")
	end
}

local obj = setmetatable({}, mt)
obj = nil
collectgarbage() -- Triggers __gc and prints the message
					

License

This project is licensed under the MIT License.