Difference between revisions of "clasp"

(Created page with "{{#set:Name=clasp}} {{#set:LOVE Version=Any}} {{#set:Description=Tiny Lua class library}} {{#set:Keyword=Class}} Minimal Lua class library in 13 lines of code: https://github...")
 
(Blanked the page)
Line 1: Line 1:
{{#set:Name=clasp}}
 
{{#set:LOVE Version=Any}}
 
{{#set:Description=Tiny Lua class library}}
 
{{#set:Keyword=Class}}
 
  
Minimal Lua class library in 13 lines of code: https://github.com/evolbug/lua-clasp <br />
 
<source lang="lua">
 
class = require "clasp"
 
 
 
-- Basic class
 
Vector = class {
 
  isVector = true; -- static values
 
 
  init = function(self, x, y) -- initializer function
 
    self.x = x
 
    self.y = y
 
  end;
 
}
 
 
a = Vector(10, 10)
 
print('Vector:', a.x, a.y, a.isVector)
 
 
 
-- Inheritance
 
Vector3 = Vector:extend {
 
  init = function(self, x, y, z) -- function overriding
 
    Vector.init(self, x, y) -- superclass method call
 
    self.z = z
 
  end;
 
}
 
 
b = Vector3(1, 2, 3)
 
print('Vector3:', b.x, b.y, b.z, b.isVector)
 
 
 
-- Metamethods
 
Point = class {
 
  init = function(self, x, y)
 
    self.x = x
 
    self.y = y
 
  end;
 
  __meta = { -- metamethod table
 
    __tostring = function(self)
 
      return 'Point('..tostring(self.x)..', '..tostring(self.y)..')'
 
    end;
 
  };
 
}
 
 
c = Point(15, 25)
 
print(c)
 
</source>
 
 
 
[[Category:Libraries]]
 

Revision as of 16:10, 23 April 2017