Comprehensive Windows framework to develop in Lua

Object oriented programming with Luart

By Samir Tine, modified on December 2021

Lua standard type system defines 8 basic types : In addition, Luart gives you the possibility to define custom types. Custom types extends the Lua type system using a light object-oriented implementation. A custom type or "Object" is defined as a table, with fields and methods. Fields and methods can be set as for tables. But unlike tables, an Object can be instantiated, to create a new value of that custom type.
By convention, all Luart Objects start with a capital letter (File , Socket,...) to differentiate from the Lua standard types.

Object definition

Objects are defined using the Object() global function. The Object global function expects one argument : a table.

Polygon = Object {      -- Defines a new Object Polygon
	sides = 0			-- Defines a 'sides' field in Polygon
print(Polygon)			-- prints 'Object: 08BDEF'
print(Polygon.sides)	-- prints '0'
An Object can be defined from an other Object, sharing the same fields and methods :

Square = Object (Polygon) -- Defines an Object Square from Polygon
print(Square.sides)		-- prints '0'
Objects have the same behavior as tables :
  • Objects can have fields of any value (strings, other Object values, functions...).
  • Field deletion is done by assigning a nil value.
  • Field access is done with ' . ', or ' : ' for methods, or '[ ]'

Object value

An Object act as a "prototype", or a "model" to create values from it. All the values of an Object share the same fields and methods. To create a new value from an Object, use the following syntax :

red_square = Square() 	-- Create a new value of type 'Square'
red_square.sides = 4    -- Set the field 'sides' with value 4
print(Square.sides)		-- prints '0' (field sides from Square )
print(red_square.sides)	-- prints '4' (field sides from red_square, a Square value)
In fact, the creation of a new value uses a special function called "constructor" to do all necessary intialization. If the Object have no constructor, a default one is internaly provided. By default, value fields are initialized with the Objects fields. A created Object value can add new fields in its constructor, and even outside of it (remember, Objects are acting like tables).

Object constructor

Object constructor is a method named 'constructor'. In the following sample, a Polygon constructor is defined to initialize the field "sides". The Square constructor defines two new fields, "size" and "color" :

function Polygon:constructor(n)		-- defines a constructor for the Object Polygon
	self.sides = n 					-- update the field 'sides' with the argument given during instantiation

function Square:constructor(color)	-- defines a constructor for the Object Square
	self.color = color				-- defines a new field 'color'
	self.size = 10					-- defines a new field 'size'
	Polygon.constructor(self, 4)	-- call Polygon constructor with argument '4' 

red_square = Square("red")	-- Instantiate a Square Object, with "red" as argument to its constructor
print(red_square.color)		-- prints 'red'
print(red_square.sides)		-- prints '4'
print(Square.sides)			-- prints '0' (field value defined in Object 'Square')
An Object constructor can be called directly with the Object value as the first argument (self).

Object destructor

Object destructor is a special method that is called when the Object instance is destroyed by the Lua Garbage collector. Please note that the destructor is called only when the garbage collection occures (assigning nil to an Object instance variable does not guarantee to call the Object destructor just after).

Object properties

Objects can have properties. Properties are virtual fields that call methods to set or get the field value. The getter method name must start with "get_" and setter method name start with "set_"

function Square:get_area()			-- defines a getter method for the property 'area'
	return self.size * self.size

Object type information

As explained before, Objects are like "prototypes". The type of an Object is "Object". The type of a value is the name of its Object. See following example to be more clear :

print(type(Polygon))	-- prints 'Object'
print(type(Square))		-- prints 'Object'
print(type(red_square)	-- prints 'Square'