Slither

Slither is a class library for lua heavily inspired by the python programming language.

Defining a class

The basic syntax used for defining a class is as follows:

class "ClassName"
{
    member = 3,

    method = function(self)
        print(self.member)
    end,

    classMethod = function()
        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.

Inheritance

If we’ve first defined a parent class like

class "ParentClass"
{
    member = 3,

    method = function(self)
        print(self.member)
    end,
}

We can then inherit from it using:

class "Subclass" (ParentClass)
{
    member = 5,
}

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

Helper functions

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.

Special methods and members

Predefined members

Operator overloading

Slither allows operator overloading, and it does this using the following methods:

Attributes

Miscellaneous features

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.