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. |
isAssignableFrom (class, subclass) | Indicates whether or not the specified subclass is the same or a sub class of the specified class. |
isInstance (class, instance) | Indicates whether or not the specified instance is an instance of the specified class. |
define (class[, defineInstanceFn[, defineClassFn]]) | Implements the specified class by setting its prototype and class methods. |
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.
- ... 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'
- 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
- 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 check.
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
- define (class[, defineInstanceFn[, defineClassFn]])
-
Implements the specified class by setting its prototype and class methods.
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
- 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