Lunatik

classModule

Minimal class helper for the OOP-style modules (e.g.

socket.inet, socket.unix). Calling the module on a table makes it an instantiable class: it gains a :new constructor that wires __index/__close and sets the class as the metatable of new objects, serving both to build instances and to derive specializations (subclasses that inherit the methods).

Usage

local class = require("class")

local Point = class{}
function Point:sum() return self.x + self.y end

local point <close> = Point:new{x = 1, y = 2}   -- point:sum() == 3

Functions

class ([class]) Makes class an instantiable class by attaching the shared :new.

Functions in detail

class ([class])
Makes class an instantiable class by attaching the shared :new. class:new(o) sets __index and __close on the class, the latter from close as it is at that call, then makes the class the metatable of o.

Parameters

  • class table the class table (defaults to a fresh table). (optional)

Returns

    table the same table, ready to receive methods and be instantiated.