Andreas Rozek Hints for Reading List of Recent Changes Guestbook Entry Contact the Author  Deutsche Version  HomePage Previous Topic Next Topic  First Page of Current Topic Next Page Previous Page

Lua_03 - Experiments with Tag-based Inheritance

"Lua_03" implements a simple method for (single) "inheritance" based on "Tag Methods". The basic principle has already been adequately described [1,2,3,4] and shall therefore only briefly be outlined here: every attempt to access a non-existent table element causes the invocation of a related tag method - assuming that such a method has been defined before. This method now looks for a "prototype" of the actual table ("object") and - within that prototype - recursively for the requested element. Only when this search fails as well, the element counts as undiscoverable.

Along with the abovementioned inheritance scheme "Lua_03" contains a number of supporting routines:

  • an enhanced type function distinguishes between "plain" tables and those supporting the described inheritance mechanism,

  • a generic clone method allows the duplication of objects,

  • an inheritsfrom method simplifies the detection of any relationships between objects (with regard to inheritance),

  • a new method allows to instantiate new objects from a given protoype. As an additional "feature", this method is installed such that it will be invoked whenever the (principally unexecutable) object itself gets called.

In order to demonstrate these functions and methods, "Lua_03" defines two prototypes which are related to each other: while an "User" object describes a single "user", another "Group" object represents an "user group". Since "User" acts as a prototype for "Group", its methods may also be applied to "Group" objects.

"Lua_03" should be invoked without any command line arguments

  lua Lua_03.lua

and yields the following output:

  Lua_03 - first experiments with tag-based (single) inheritance

  firstUser  = Rozek, Elena
  secondUser = Rozek, Andreas
  Phantom    = (unknown), (unknown)
  UserGroup  = Rozek Family [Rozek, Elena; Rozek, Andreas]

  UserGroup:inheritsfrom(User)? 1
  UserGroup:inheritsfrom(Object)? 1

  type(UserGroup) = object

It is recommended to analyze this output along with the source code - this clarifies the proper behaviour of the abovementioned functions and methods.

Source Code

The source code of this example is available for download:

As the functions defined within "Lua_03.lua" will also be used in other programming examples, they may be downloaded separately as a Lua script file:

References

[1] Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
Reference Manual of the Programming Language Lua 4.0
(see http://www.lua.org/manual)
the official reference manual contains any relevatn information about the language itself, the set of standard libraries and its interface to the run-time environment;
[2] Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes Filho
Lua - an extensible extension language
Software: Practice & Experience (No. 6, Vol. 26, 1996), p. 635-652
(see http://www.lua.org/spe.html)
this paper describes the fundamental characteristics of Lua (and its integration into applications written in C/C++), sometimes backed by source code examples;
[3] Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
Lua: an Extensible Embedded Language
A few metamechanisms replace a host of features
Dr. Dobb's Journal (No. 12, Vol. 21, December 1996), p. 26-33
(see http://www.lua.org/ddj.html)
this article describes the most important characteristics of Lua and TkLua. A number of source code examples illustrate the given descriptions;
[4] Reuben Thomas et al.
Technical Notes
(see http://www.lua.org/notes)
the (extensible) series of "Technical Notes" describes particular language- and application-specific issues related to Lua;

Disclaimer

Please, consider also the author's Disclaimer!

http://www.Andreas-Rozek.de/Lua/Acquainting/Lua_03_en.html    (last Modification: 15.04.2002)