LOS - Lua Object System
What the heck is LOS?
LOS is a object system writed in pure Lua.
What are the advantages of LOS?
=> Mixins (Virtual Multiple Inheritance)
LOS implements suport to virtual multiple inheritance using mixins like Ruby.
=> Classes
LOS implements support to classes with a embedded typing system.
=> Method Overloading
LOS implents a simple method overload.
=> Inheritance (single)
LOS support single inheritance like Java.
=> Multiple Class Importation (In Development)
LOS support multiple class loading of a same class path like Python.
Documentation:
class
Create a class and register the created class into _G[], the created class is converted in a Lua table where:
is the name of class registred into _G.
mixin
Add all methods of a module or class in your class where:
is the name of loaded class to mix in your class.
extends
Make inheritance of a class in your class where:
is the name of loaded class to inherit.
overload
Overload a method of a LOS where:
is the name of method
is the new method
is the number of parameters of new method only 1 method can be overloaded foreach number of parameters.
Examples:
-- point.lua
require 'LOS'
-- Calling the "class" function and passing a class name as
-- string the class function create a class for you.
class 'Point'
-- This method is used in inheritance of variables of a class
-- then use this method to declare variables and set your
-- default values.
function initialize()
self.x = 0
self.y = 0
end
-- This is the constructor of class used to instantiate the class
-- is permited only one constructor by class.
function Point(x, y)
self.x = x
self.y = y
end
-- The variable "this" and the variable "my" are references
-- to your class.
function this:add(point)
if point:instanceof 'Point' then
self.x = self.x + point.x
self.y = self.y + point.y
end
return self
end
function my:draw()
print("X = ", self.x, " Y = ", self.y)
end
-- point3d.lua
require "point"
-- Using the function "extends" to makes inheritance in a class.
class 'Point3D' extends 'Point'
function initialize()
self.z = 0
end
function Point3D(x, y, z)
this.Point(x, y)
self.z = z or 0
end
function my:draw()
print("X = ", self.x, " Y = ", self.y, " Z = ", self.z)
end
-- point4d.lua
require "point3d"
require "axis"
-- The "mixin" function add to your class ALL methods in the
-- class or module passed to function.
class 'Point4D' extends 'Point3D' mixin 'Axis'
function initialize()
self.a = 0
end
-- Calling the super class constructor you can
-- set variables in your instance
function Point4D(x, y, z, a)
super.Point3D(x, y, z)
self.a = a
end
function my:draw()
print("X = ", self.x, " Y = ", self.y, " Z = ", self.z, " A = ", self.a)
end
-- axis.lua
module('Axis', package.seeall)
function axis()
print(self.a, "Hi! I'm a mixin!")
end
-- main.lua
require 'axis'
require 'point'
require 'point3d'
require 'point4d'
local point = Point:new(3,5)
local point3d = Point3D:new(3, 4, 9)
local point4d = Point4D:new(3, 4, 9, 8)
point:draw()
point3d:draw()
point:add(point4d1):draw()
point4d:draw()
point4d:axis() |