Module jls.lang.class

Provides class creation with inheritance and constructor.

This module provides helper functions to create and work with classes. A class can implement prototype methods shared among all its instances. A class can implement an initialize method that will be called for new instances. A class can inherit from another class, prototype methods are inherited by the subclasses.

Usage:

    local class = require('jls.lang.class')
    local Person = class.create(function(person)
      function person:initialize(name)
        self.name = name
      end
      function person:getName()
        return self.name
      end
    end)
    local luke = Person:new('Luke')
    
    local User = class.create(Person, function(user, super)
      function user:initialize(name, registrationYear)
        super.initialize(self, name)
        self.registrationYear = registrationYear
      end
    end)
    local dave = User:new('Dave', 2012)
    

Functions

getClass (instance) Returns the class of the specified instance.
cloneInstance (instance) Returns a copy of the specified instance.
newInstance (class, ...) Creates a new instance of the specified class.
getName (class) Returns the name of the specified class.
isAssignableFrom (class, subclass) Indicates whether or not the specified subclass is the same or a sub class of the specified class.
isClass (value) Returns true if the specified value is a class.
isInstance (class, instance) Indicates whether or not the specified instance is an instance of the specified class.
asInstance (class, ...) Returns an instance of the specified class.
define (class[, defineInstanceFn[, defineClassFn]]) Implements the specified class by setting its prototype and class methods.
modifyInstance (instance, fn) Modifies the specified instance.
create ([super][, defineInstanceFn[, defineClassFn]]) Returns a new class inheriting from specified base class.


Functions

getClass (instance)
Returns the class of the specified instance.

Parameters:

  • instance The instance to get the class from

Returns:

    The class of the specified class or nil if there is no such class

Usage:

    local Vehicle = require('jls.lang.class').create()
    local car = Vehicle:new()
    car:getClass() -- Returns Vehicle
cloneInstance (instance)
Returns a copy of the specified instance. This method performs a shallow copy, field-by-field copy, not a deep copy.

Parameters:

  • instance The instance to clone

Returns:

    A copy of the specified instance, or nil if the instance has no class

Usage:

    local Vehicle = require('jls.lang.class').create()
    local car = Vehicle:new()
    local carCopy = car:clone()
newInstance (class, ...)
Creates a new instance of the specified class.

Parameters:

  • class The class to instantiate
  • ... The parameters passed to the initialize method

Returns:

    A new instance

Usage:

    local Vehicle = require('jls.lang.class').create(function(vehicle)
      function vehicle:initialize(color)
        self.color = color
      end
      function vehicle:getColor()
        return self.color
      end
    end)
    local car = Vehicle:new('blue')
    car:getColor() -- Returns 'blue'
getName (class)
Returns the name of the specified class.

Parameters:

  • class The class to look for

Returns:

    string The class name or nil if the class is not found in package.loaded
isAssignableFrom (class, subclass)
Indicates whether or not the specified subclass is the same or a sub class of the specified class.

Parameters:

  • class The class to check with
  • subclass The class to be checked

Returns:

    true if the subclass is the same or a sub class of the class, false otherwise

Usage:

    local class = require('jls.lang.class')
    local Vehicle = class.create()
    local Bus = class.create(Vehicle)
    Vehicle:isAssignableFrom(Bus) -- Returns true
isClass (value)
Returns true if the specified value is a class.

Parameters:

  • value The class to check

Returns:

    true if the specified value is a class, false otherwise
isInstance (class, instance)
Indicates whether or not the specified instance is an instance of the specified class.

Parameters:

  • class The class to check with
  • instance The instance to be checked

Returns:

    true if the instance is an instance of the class, false otherwise

Usage:

    local Vehicle = require('jls.lang.class').create()
    local car = Vehicle:new()
    Vehicle:isInstance(car) -- Returns true
asInstance (class, ...)
Returns an instance of the specified class.

Parameters:

  • class The class from which an instance is expected
  • ... The instance to be checked or the creation parameters

Returns:

    An instance
define (class[, defineInstanceFn[, defineClassFn]])
Implements the specified class by setting its prototype and class methods. The following methods are automatically set in the metatable: equals as __eq, length as __len, pairs as __pairs, toString as __tostring.

Parameters:

  • class The class to implement
  • defineInstanceFn function An optional function that will be called with the class prototype to implement (optional)
  • defineClassFn function An optional function that will be called with the class (optional)

Returns:

    The class

Usage:

    local class = require('jls.lang.class')
    local Person = class.create()
    class.define(function(person)
      function person:initialize(name)
        self.name = name
      end
      function person:getName()
        return self.name
      end
    end, function(Person)
      function Person:getDefaultHeight()
        return 1.75
      end
    end)
    local User = class.create(Person)
    class.define(function(user, super)
      function user:initialize(name, registrationYear)
        super.initialize(self, name)
        self.registrationYear = registrationYear
      end
    end)
    local john = User:new('john', 2011)
    john:getName() -- Returns 'john'
    User:getDefaultHeight() -- Returns 1.75
modifyInstance (instance, fn)
Modifies the specified instance. This method allows to override class methods for a specific instance.

Parameters:

  • instance The instance to modify
  • fn function The function that will be called with the instance and its prototype

Returns:

    The modified instance
create ([super][, defineInstanceFn[, defineClassFn]])
Returns a new class inheriting from specified base class. The class is implemented using the specified functions by calling define. The class has a new method to create new instance and a isInstance method to check compatibility.

Parameters:

  • super An optional base class to inherit from, could be the class name as a string (optional)
  • defineInstanceFn function An optional function that will be called with the class prototype (optional)
  • defineClassFn function An optional function that will be called with the class (optional)

Returns:

    A new class

Usage:

    local Vehicle = class.create()
    local Car = class.create(Vehicle)
    local car = Car:new()
    Vehicle:isInstance(car) -- Returns true
generated by LDoc 1.4.6