Slither is a class library for lua heavily inspired by the python programming language.
The basic syntax used for defining a class is as follows:
"ClassName"
class {
= 3,
member
= function(self)
method print(self.member)
end,
= function()
classMethod print("Class member!")
end,
}
This creates a class named ClassName
, and sets a global variable named ClassName
to point to it. The class “keyword” also returns the newly created class. The prototype for this class is the table constructed (so whatever’s between the curly braces).
In this case, an object contains a field member
with value 3, a method method
that prints said value, specifically the object instance of it, and a method on the class classMethod
.
If we’ve first defined a parent class like
"ParentClass"
class {
= 3,
member
= function(self)
method print(self.member)
end,
}
We can then inherit from it using:
"Subclass" (ParentClass)
class {
= 5,
member }
Subclass
will have inherited any (class/object) members (including methods) from ParentClass
, overriding anything it contains itself (member
in this case.)
Slither also supports multiple inheritance, using a well-defined order in which lookup is performed, which is left-to-right in order or definition, depth-first. As an example, having the following hierarchy:
A B C D
\ | / |
E F
\ /
\ /
G
A lookup in G
will do it in the following order: G, E, A, B, C, F, D
Slither also defines the functions issubclass
and isinstance
, which determine recursively whether a class is derived from another, or an object is an instance of a class, or its subclasses, respectively.
issubclass
allows for the following invocations:
boolean = issubclass(class, parent)
boolean = issubclass(class, {parents...})
isinstance
allows for the following invocation:
boolean = isinstance(object, parent)
boolean = isinstance(object, {parents...})
In general, isinstance
is equivalent to calling issubclass
with the same arguments, substituting the object for its class.
__class__
Returns the class this object is an instance of.__name__
Returns the name of this object’s class.Slither allows operator overloading, and it does this using the following methods:
__cmp__
: Overrides most comparison operators, gets called with two arguments, return 0 when they are equal, a negative number if a is less than b, and a positive number otherwise.__call__
: Gets called when the object is called like a function.__len__
: Override the length returned using the length (#
) operator.__add__
: Overrides addition.__sub__
: Overrides subtraction.__mul__
: Overrides multiplication.__div__
: Overrides division.__mod__
: Overrides the modulo operation.__pow__
: Overrides the power (^
) operator.__neg__
: Overrides the unary minus (or negation).__getattr__
: Overrides the return value of an undefined index operation.__setattr__
: Overrides the result of setting an undefined member.__attributes__
: A list of callables that are called on the created class object (not instances), allowing for modification and replacement. Not inherited.This library has support for Class Commons, a “standard” for lua class libraries that allows libraries to target multiple class libraries, and provide native classes for their users.