Lua classes

The classes used in this project use a base structure as defined in the Object Orientation Tutorial at the lua-users wiki. There are three different variations on this theme; base classes and derived classes which are quite similar to the class structure described in the section of “Inheritance”, and an inheritance-less design which are similar to the class structure described in the section “Simple metatable-based class”.

The only place where closure is used to keep state is during initial setup, in Pickle.lua, where state would otherwise be lost. That is although a module and not a class.

Class creation

New instances are created from a method create() that can be called on loaded modules and on preexisting instances. (TODO: Full cleanup from previous function-like creation is not yet done.) Specialization of the newly created instance is done in an _init() method called immediately after creation.

This pattern implies that libA:create() will create a new instance of the type defined by libA, but also that objA:create() will create an instance of same type as objA. In the later case it will although not be a clone of objA. It is also possible to call the method as a library function, like libA.create(). This is a common pattern in older lua code.

Method lookup

To make virtual methods work properly the metamethod __index() is used instead of tables. This is just a plain redirect and is similar in all classes. It will also avoid problems otherwise created by memory loops in the lua environment.

Redirection through methods like this is somewhat heavy and could be rewritten as a table lookup if online testing is too sluggish.

Call style

Some classes have a metamethod __call() that hides much of the instance creation process. The general idea is to minimize the burden during reuse. Strategies uses the call style to simplify access, so Renders and Extractors has __call() metamethods.

Methods that rely on argument chaining, those that sink one argument before going after the next one, like Adapt and Case, use __call() to implement the process. They sink one argument, return an object that has a call method, and calls that to sink yet another one.

In addition to these classes, a few others has such methods such as Counter.

generated by LDoc 1.4.6