lux.class a LUX prototype

A class-based implementation object oriented programming. Ironically, this is actually a prototype, which means it inherits from lux.prototype, but otherwise provides its own mechanism for OOP. Be sure to check instance, inherit and super usages.

This module requires macro takeover in order to work properly

Usage:

    local MyClass = require 'lux.class' :new{}

Functions

class:instance (obj, ...)

Defines how an instance of the class should be constructed. This function is supposed to only be overriden, not called from the user’s side. By populating the _ENV parameter provided in this factory-like strategy method is what creates class instances in this OOP feature. This is actually done automatically: every “global” variable or function you define inside this function is instead stored as a corresponding object field.

Parameters:

  • object obj

    The to-be-constructed object. On Lua5.2+, you may name it _ENV if you know what you are doing.

  • any ...

    Arguments required by the construction of objects from the current class

Usage:

      local MyClass = require 'lux.class' :new{}
      local print = print -- must explicitly enclosure dependencies
      function MyClass:instance (obj, x)
        -- public field
        obj.y = 1337
        -- private field
        local a_number = 42
        -- public method
        function obj.show ()
          print(a_number + x)
        end
      end
    
      myobj = MyClass(8001)
      -- call without colons!
      myobj.show()

See also:

class:inherit (another_class)

Makes this class inherit from another. This guarantess that instances from the former are also instances from the latter. The semantics differs from that of inheritance through prototyping! Also, it is necessary to call super inside the current class' instance definition method since there is no way of guessing how the parent class' constructor should be called.

Parameters:

  • class another_class

    The class being inherited from

Usage:

      local class = require 'lux.class'
      local ParentClass = class:new{}
      local ChildClass = class:new{}
      ChildClass:inherit(ParentClass)

See also:

class:__call (...)

The class constructor. This is how someone actually instantiates objects from this class system. After having created a new class and defined its instance method, calling the class itself behaves as expected by calling the constructor that will use the instance method to create the object.

Parameters:

  • any ...

    The constructor parameters as specified in the instance

Returns:

    object

    A new instance from the current class.

class:super (obj, ...)

Calls the parent class' constructor. Should only be called inside this class' instance definition method when it inherits from another class.

Parameters:

  • object obj

    The object being constructed by the child class, that is, the _ENV parameter passed to instance

  • any ...

    The parent class' constructor parameters

Usage:

      -- After ChildClass inherited ParentClass
      function ChildClass:instance (obj, x, y)
        self:super(obj, x + y) -- parent's constructor parameters
        -- Finish instancing
      end

See also: