Module subclass

Class factory.

Source

Source code can be found here.

User's guide

  • a class table, or a class - is a table that defines a class. This table can be called to create instances of that class. Returned by the Class:subclass method.
  • an instance table, or an instance, is a table that represents an instance of a class
  • the Class:new method is the constructor of the class. Called when an instance is created
  • class.super or instance.super refers to the superclass table (if any)

To create an instance of class A you call A with a table as the only parameter. This table is accessible then inside the A:new method as self:

local A = Class:subclass('A')
function A:new()
    print(('From A:new() : self.foo = %s'):format(self.foo))
end

local a = A { foo = 'bar' }
-- > From A:new() : self.foo = bar

When an instance is created all constructors in the hierarchy are called in the order from parent to child:

local A = Class:subclass('A')
function A:new()
    print('A:new()')
end

local B = A:subclass('B')
function B:new()
    print('B:new()')
end

local C = B:subclass('C')
function C:new()
    print('C:new()')
end

local c = C { foo = 'bar' }
-- > A:new()
-- > B:new()
-- > C:new()

Class Class

Class:subclass (name) Creates a subclass of a class.
Class.new (_) Constructor.
Class:isSubclassOf (other) Checks whether a given class is a subclass of another one.
Class:isA (other) Checks whether a given object is an instance of a class.


Class Class

Represents a class.
Class:subclass (name)
Creates a subclass of a class.

Parameters:

  • name Must be a string. This is the name of new class.

Returns:

    New class (a table).

Usage:

    local Class = require('subclass') -- this module
    
    local Animal = Class:subclass('Animal') -- a class, or a class table
    local Dog    = Animal:subclass('Dog') -- also a class
    local bobik  = Dog { name = 'bob' } -- an instance of the Dog class
Class.new (_)
Constructor. Called when an instance of a class is created. The instance created is accessible inside this method as self. Note: there is no need to return anything from that method.

Parameters:

  • _ The self parameter, the instance itself. Note: the dot syntax here instead of familiar colon one because of two reasons: a) I want to explicitely document the self parameter and b) the linter complains about not used parameter if :new() is used.

Usage:

    local Animal = Class:subclass('Animal')
    
    function Animal:new()
        print('Animal:new()')
        self.hp    = self.hp or 100
        self.alive = true
    end
    
    local Dog = Animal:subclass('Dog')
    
    function Dog:new()
        print('Dog:new()')
        self.hasTail = true
    end
    
    local bobik = Dog { name = 'bob', hp = 200 }
    -- > Animal:new()
    -- > Dog:new()
    
    assert(bobik.alive == true)
    assert(bobik.hasTail == true)
    assert(bobik.hp == 200)
    assert(bobik.name == 'bob')
Class:isSubclassOf (other)
Checks whether a given class is a subclass of another one. Note: self here must be a class - not an instance. To check whether an instance is a type of a class use Class:isA.

Parameters:

  • other A class table

Returns:

    true if a class is a subclass of another class. false otherwise.

Usage:

    local Animal = Class:subclass('Animal')
    local Dog    = Animal:subclass('Dog')
    local Cat    = Animal:subclass('Cat')
    
    Dog:isSubclassOf(Animal) --> true
    Cat:isSubclassOf(Animal) --> true
    Cat:isSubclassOf(Dog) --> false
Class:isA (other)
Checks whether a given object is an instance of a class. Note: self here must be an instance - not a class. To check whether a class is a subclass of another one use isSubclassOf.

Parameters:

  • other A class table

Returns:

    true if the object is an instance of a given class. false otherwise.

Usage:

    local Animal = Class:subclass('Animal')
    local Dog    = Animal:subclass('Dog')
    
    local bobik = Dog { name = 'bob' }
    
    bobik:isA(Animal) --> true
    bobik:isA(Dog) --> true
generated by LDoc 1.5.0 Last updated 2025-10-05 08:47:19