| Andreas Rozek |
|
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:
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 CodeThe 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
|
| http://www.Andreas-Rozek.de/Lua/Acquainting/Lua_03_en.html | (last Modification: 15.04.2002) |