Skip to content

evolbug/lua-clasp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-clasp

Tiny Lua class libary

Usage

Basic class

class = require "clasp"

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) -- "Vector: 10 10 true"

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) -- "Vector3: 1 2 3 true"

Metamethods

Point = class {
  init = function(self, x, y)
    self.x = x
    self.y = y
  end;
  __ = { -- metamethod table
    tostring = function(self)
      return 'Point('..tostring(self.x)..', '..tostring(self.y)..')'
    end;
  };
}

c = Point(15, 25)
print(c) -- "Point(15, 25)"

About

tiny lua class libary

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages