File LCS.lua

Functions

LCS.class (args) Creates a class
LCS.class.abstract (args) Creates an abstract class.
LCS.class.final (args) Creates a final class.
LCS.is_A (thing, kind) Checks the nature of the given argument 'thing'.
[class] (args) Default class constructor used for instantiation.
[class]:extends (args) Returns a new class derived from class [class].
[class]:getClass () Returns a reference to the superclass of class [class]
[class]:getSubClasses () Returns a list of all classes deriving from class [class]
[class]:new (args) Default class constructor used for instantiation.
[class]:super (method, ...) Calls a method defined in a parent of class [class]
[instance]:getClass () Returns a reference to the class from which 'instance' was created
[instance]:is_A (aClass, shallow) Checks if [instance] was created from a specific class.
[instance]:super (method, ...) Calls a methods defined in a parent of a class from which object 'instance' was created


Functions

LCS.class (args)
Creates a class

Parameters

  • args: [table] Optional.A key-index table to be the class members.

Usage:

 
LCS = require('LCS')
Vector = LCS.class { x = 0, y = 0}

Return value:

[table] A class
LCS.class.abstract (args)
Creates an abstract class. These classes cannot instantiate objects.

Parameters

  • args: [table] Optional.A key-index table to be the class members.

Usage:

 
LCS = require('LCS')
myAbstractClass = LCS.class.abstract()

Return value:

[table] An abstract class
LCS.class.final (args)
Creates a final class. They do not offer support for inheritance. So these classes cannot be derived.

Parameters

  • args: [table] Optional.A key-index table to be the class members.

Usage:

 
LCS = require('LCS')
myFinalClass = LCS.class.final()

Return value:

[table] A final class
LCS.is_A (thing, kind)
Checks the nature of the given argument 'thing'.

Parameters

  • thing: [table] The reference holding a supposed object or a supposed class to be tested
  • kind: [string] Optional. A string value to be either 'object' or 'class'. When not given, returns a string representing the nature of the given 'thing'

Usage:

 
LCS = require('LCS')
myClass = LCS.class()
myObject = myClass()
print(LCS.is_A(myClass,'class')) --> True
print(LCS.is_A(myObject,'object')) --> True
print(LCS.is_A(myClass)) --> 'class'
print(LCS.is_A(myObject)) --> 'object'

Return value:

[boolean] or [string] True if 'thing' matches string 'kind', or the string 'object' or 'class'
[class] (args)
Default class constructor used for instantiation. Similar to [class]:new(). Assumes the calling class is not abstract.

Parameters

  • args: [table] Optional.A list of arguments in a specific order to init the new instance. Using args assumes the constructor [class]:init() have been implemented upon class definition.

Usage:

 
LCS = require('LCS')
Vector = LCS.class {x = 0, y = 0}
function Vector:init(x,y)
self.x = x
self.y = y
end
v1 = Vector(1,2)
print(v1.x,v1.y) --> 1,2

Return value:

[table] An instance of class [class]

See also:

[class]:extends (args)
Returns a new class derived from class [class]. Assumes class [class] is not a final class.

Parameters

  • args: [table] Optional. A key-index table to be the class members.

Usage:

 
LCS = require('LCS')
Animal = LCS.class()

function Animal:yell()
print('Yelling!')
end

Dog = Animal:extends {name = nil}
function Dog:init(name)
self.name = name
end

puppet = Dog('waffles')
print(puppet.name) --> waffles
puppet:yell() --> Yelling!

Return value:

[table] A new class which inherits from class [class]

See also:

[class]:getClass ()
Returns a reference to the superclass of class [class]

Usage:

 
LCS = require('LCS')
Animal = LCS.class()

Dog = Animal:extends {name = nil}
print(Dog:getClass() == Animal) --> True

Return value:

[table] or [nil] Returns a reference to the superclass of class [class]. If [class] does not derives from a class, returns nil

See also:

[class]:getSubClasses ()
Returns a list of all classes deriving from class [class]

Usage:

 
LCS = require('LCS')
Animal = LCS.class()

Dog = Animal:extends {name = nil}
list = Animal:getSubClasses()
print(list[Dog]) --> True

Return value:

[table] Returns a list of all classes deriving from class indexed with these classes[class]
[class]:new (args)
Default class constructor used for instantiation. Similar to [class](). Assumes the calling class is not abstract.

Parameters

  • args: [table] Optional.A list of arguments in a specific order to init the new instance. Using args assumes the constructor [class]:init() have been implemented upon class definition.

Usage:

 
LCS = require('LCS')
Vector = LCS.class {x = 0, y = 0}
function Vector:init(x,y)
self.x = x
self.y = y
end
v1 = Vector:new(3,4)
print(v1.x,v1.y) --> 3,4

Return value:

[table] An instance of class [class]

See also:

[class]:super (method, ...)
Calls a method defined in a parent of class [class]

Parameters

  • method: [string] A string refferring to the name of the function to be invoked.
  • ...: Optional. The required arguments which will be passed to the method called, in proper order.

Usage:

 
LCS = require('LCS')
Dog = LCS.class {name = ''}
function Dog:init(name)
self.name = name
end
SuperDog = Dog:extends {power = 0}
function SuperDog:init(name,power)
self:super('init',name)
self.power = power
end

superPuppet = SuperDog('superPuppet',100)
print(superPuppet.name) --> superPuppet
print(superPuppet.power) --> 100

Return value:

[...] The result of expected from the method call.

See also:

[instance]:getClass ()
Returns a reference to the class from which 'instance' was created

Usage:

 
LCS = require('LCS')
Animal = LCS.class()

Dog = Animal:extends {name = nil}
puppet = Dog()
print(puppet:getClass() == Dog) --> True

Return value:

[table] Returns a reference tto the class from which 'instance' was created

See also:

[instance]:is_A (aClass, shallow)
Checks if instance [instance] was created from a specific class. This method should not be called from a class.

Parameters

  • aClass: [table] Optional. When given, tests if 'instance' was instantiated from aClass.
  • shallow: [boolean] Optional. When given, will recurse on each parent class in the inheritance tree of the calling object

Usage:

 
LCS = require('LCS')
Animal = LCS.class()

thing = Animal()
print(thing:is_A(Animal)) --> True
print(thing:is_A() == Animal)--> True
 
local LCS = require 'LCS'
local class  = LCS.class()
local extClass = class:extends()

local ext0 = extClass:extends()
local ext1 = ext0 :extends()
local ext2 = ext1:extends()
local obj = ext2()
    
print(LCS.is_A(obj,'object')) --> true
   
print(obj:is_A(ext2)) --> true
print(obj:is_A(ext1)) --> true
print(obj:is_A(ext0)) --> true
print(obj:is_A(class)) --> true
    
print(obj:is_A(ext2,true)) --> true
print(obj:is_A(ext1,true)) --> false
print(obj:is_A(extO,true)) --> false
print(obj:is_A(class,true)) --> false

Return value:

[boolean] or [table] Returns True if 'instance' was instantiated from aClass, either False. If 'aClass' is not given, returns a reference to the class from which 'instance' was created.

See also:

[instance]:super (method, ...)
Calls a methods defined in a parent of a class from which object 'instance' was created

Parameters

  • method: [string] A string refferring to the name of the function to be invoked.
  • ...: Optional. The required arguments which will be passed to the method called, in proper order.

Usage:

 
LCS = require('LCS')
Dog = LCS.class {name = ''}
function Dog:init(name)
self.name = name
end
function Dog:say(something)
print(something)
end
SuperDog = Dog:extends {power = 0}
function SuperDog:say(something)
print('Superdog says : '..something)
end
superPuppet = SuperDog()
superPuppet:say('Hello!') --> SuperDog says : Hello!
superPuppet:super('say','Hello!') --> Hello!

Return value:

[...] The result of expected from the method call.

See also:

Valid XHTML 1.0!