Skip to content

Repository files navigation

lua-pie

Table of contents

Overview

lua-pie (polymorphism, ineritance and encapsulation) is a class library for Lua. Currently lua-pie supports interfaces with abstract methods and classes with private, public and static methods as well as inheritance with polymorphism via the respective keywords. Private member variables can be declared with the self keyword in the constructor. Classes may also contain metamethods using the operator keyword.

Installation

You can download lua-pie from luarocks with

luarocks install lua-pie

Or you can just clone or download this repository from github and use lua-pie.lua in your project.

Usage

Writing classes

Classes are created with the class keyword followed by the name of the class and a table containing method definitions. Method definitions are wrappend in a public, private or static block. Currently the static block is the only one allowed to contain non-function values.

localpie=require"lua-pie"localclass=pie.classlocalpublic=pie.publiclocalprivate=pie.privateclass"Greeter" {
public {
say_hello=function(self, name)
self:private_hello(name)
end
};
private {
private_hello=function(self, name)
print("Hello "..name)
end
}
}

Instantiating objects

Classes are imported via the import function. After that they can be called to create a new instance. If a constructor function is defined in the public block it will be called when creating the object.

localpie=require"lua-pie"localimport=pie.importlocalGreeter=import("Greeter")
localgreeter=Greeter()
greeter:say_hello("World")
-- Output: Hello Worldgreeter:private_hello("World")
-- Output: Error. Trying to access private member private_hello

Inheritance

lua-pie allows single inheritance. A class can extend another class by using the extends function within the class definition.

localpie=require"lua-pie"localclass=pie.classlocalpublic=pie.publiclocalprivate=pie.privateclass"Person" {
extends"Greeter";
public {
constructor=function(self, name)
self.name=nameend;
introduce=function(self)
print("Hi! My name is "..self.name)
end;
};
}
localPerson=import("Person")
localslim=Person("Slim Shady")
slim:introduce()
-- Output: Hi! My name is Slim Shadyslim:say_hello("World")
-- Output: Hello World

Interfaces

lua-pie also provides support for interfaces. Interfaces may only contain abstract functions. All functions are public by default. A class can implement an interface by using the implements function within the class definition. The argument to the implements function can either be a single class name as string or a table of class names. If a class does not implement all functions defined by an interface an error will be thrown.

localpie=require"lua-pie"localinterface=pie.interfacelocalimplements=pie.implementslocalabstract_function=pie.abstract_functionlocalclass=pie.classlocalpublic=pie.publiclocalprivate=pie.privateinterface"IGreeter" {
say_hello=abstract_function("self", "name")
}
interface"IShouter" {
shout=abstract_function()
}
class"Greeter" {
implements {
"IGreeter",
"IShouter"
};
public {
say_hello=function(self, name)
self:private_hello(name)
end;
shout=function(self)
print("I CAN SHOUT REALLY LOUD!")
end
};
private {
private_hello=function(self, name)
print("Hello "..name)
end;
}
}

Polymorphism

Methods can be overriden in subclasses, thus enabling polymorphism. Moreover every derived class has a field self.super which is an instance of the super class. The super class instance will be created using the same arguments passed into the derived class constructor.

class"Person" {
extends"Greeter";
public {
constructor=function(self, name)
self.name=nameend;
introduce=function(self)
print("Hi! My name is "..self.name)
end;
say_hello=function(self, name)
self.super:say_hello(name)
print("Hello Override")
end;
};
}
localPerson=import("Person")
localslim=Person("Slim Shady")
slim:say_hello("World")
-- Output: -- Hello World-- Hello Override

Operators

Operators (metamethods) are defined within the operators block. Currently the following operators are supported:

  • __add
  • __sub
  • __mul
  • __div
  • __pow
  • __concat
  • __unm
  • __tostring
  • __call
-- Number has to be declared first, so we can use it within the classlocalNumberNumber=class"Number" {
public {
constructor=function(self, value)
self.value=valueend;
getValue=function(self)
returnself.valueend;
};
operators {
__add=function(self, n2)
returnNumber(self.value+n2:getValue())
end;
__tostring=function(self)
returntostring(self.value)
end
}
}

Utilities

lua-pie also provides a few simple utility functions, the first being the function is. is takes an object and a class or type name as string and will return true if the given object is an instance of the specified type or class.

localslim=Person("Slim Shady")
print(is(slim, "Person"))
-- trueprint(is("string", "string"))
-- true

In general lua-pie does not allow writing new indices to object. However this behavior can be turned off to enable working with test frameworks like busted.

require'busted.runner'()
require"examples"localpie=require("lua-pie")
localimport=pie.importdescribe("When testing busted spies", function()
it("should work", function()
pie.allow_writing_to_objects(true)
pie.show_warnings(false) -- if not turned off, lue-pie will warn the user about writing to objectslocalPerson=import("Person")
localslim=Person("Slim Shady")
locals=spy.on(slim, "introduce")
slim:introduce()
assert.spy(s).was.called()
end)
end)

Important Notes

Due to the way the module is written, class methods can not be passed around like usual functions. In lua-pie when functions are called they are wrapped in a special wrapper function to pass in the private object table that is hidden from the user. When simply retrieving a function from an object it will always return the wrapper function. Below is a simple example:

localGreeter=import"Greeter"localPerson=import"Person"greeter=Greeter()
person=Person("A")
localfunc=greeter.say_hellolocalfunc2=person.introduceprint(func==func2)
-- true

Therefore if you want to pass functions around, always wrap them in another function first:

localGreeter=import"Greeter"greeter=Greeter()
localfunc=function(name) greeter:say_hello(name) endfunc("World")

About

A class library for Lua

Topics

Resources

Stars

21 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages