Exercises, examples and other material relating to training module U107. This topic is presented on public courses
|Articles and tips on this subject||updated|
|4273||Dot or Colon separator between table name and member in Lua - what is the difference?|
When do I use a dot and when do I use a colon in referencing a table member in Lua? It's a good question and one that we answer (and demonstrate to help make clear) on our Lua Programming Course.
Dot refers to a table member. So if I write
I am saying "run the read function ...
|4248||Metatables, Metamethods, classes and objects in Lua|
In Lua, almost everything (!) is held as a table. You don't have lists, hashes, dicts, arrays, objects, namespaces - you have tables. It helps consistency, in helps keep the language small - but at times it does mean you have to be a little bit more aware of how the features of tables can be combined ...
|4117||Is Lua an Object Oriented language?|
What makes an "Object Oriented Language"? Well - there are certain features you need, such as references, type handing, inheritance support and polymorphism if you're going to write code in a completely OO way, but there are also ways of following the OO paradigm in significant parts with just about ...
|3730||What is a metatable? How do I set one up? How do I use them? Lua|
What is a metatable?
In Lua, you can hold a whole bundle of information about something in a table, which gives you a convenient "handle" to that information - be that information in the form of values, or behaviours (i.e. conventional variable of code). This is illustrated in earlier articles on Lua ...
|3727||Using Lua tables as objects|
Since tables in Lua can hold data members of different types, they can be used as the base structure for an Object Oriented design approach, where each table represents an object with numbers, strings, other tables ("objects") and callable blocks ("functions" or methods) as members. And if you take ...
|3694||Special __ methods you can use in Lua metatables|
In Lua, you can associate an additional table (known as a metatable) with each table of data and code you have. By doing so, repeated values and functions that apply across as whole series ("type", "class") of tables only have to be defined once. A very neat way for Lua to provide the OO facilities ...
|3683||Weak references in Lua - what are they, and why use them?|
In programming languages where memory is allocated dynamically (that applies to most of the languages we teach), there has to be a mechanism to gather up memory that is no longer used as the program is running so that it can be re-allocated later within the same program, and that mechanism is usually ...
|3524||Metaclasses (Python) and Metatables (Lua)|
"A Metaclass (Python) or Metatable (Lua) adds extra code / data / behaviour to an object, table, or class."
What does that mean? It means that I can define a behavior that I want to be shared, that I want to define at run-time, that I want to amend ... in a separate module / zone / table from my main ...
|3396||Tables as Objects in Lua - a gentle introduction to data driven programming|
In Lua, everything that's a collection is held in a table. During the Lua Course I have been giving this week, I set up a whole series of tables, object-like, to hold details of each station on the British main rail network. As well as data, the table holds code / code references, and by adding different ...
|3142||Private and Public - and things between|
A public train service is one which is available for anyone to travel on; a private one only takes limited passengers as invites / made available by the operator. And there could be intermediate levels too - I understand that at times the British Transport Police protect the last train from Weymouth ...
|2710||__index and __newindex in Lua - metatable methods|
When you call up an element in a table in Lua, it's like accessing an array element - and it works well. But you can go further by associating a metatable with a table - that's another table with a whole lot of extras - and that allows you to alter the behaviour of the original table. Since Lua works ...
|2701||Is Lua an Object Oriented language?|
Lua isn't an object oriented languages in the sense that you define classes, create objects with a new method, define interfaces and declare variables to be private or protected ... but never the less is a tool in which you can use object oriented techniques with (indeed) a freedom and flexibility that ...
In a recent article ([here]), I asked if Lua was an object oriented language - and I concluded that it supports the OO paradigm very well, even though it doesn't have keywords like "class", "private" or "extends". At the end of that article, I mentioned metatables - a subject I have carefully sidestepped ...
|2455||Lua examples - coroutines, error handling, objects, etc|
I have presented a bespoke Lua course over the last three days ... and it has lead me to some interesting new examples which I'm sharing here.
Object Orientation is more a way of thinking and coding than a language feature in Lua - when you set up a piece of data / structure / object, you do so in a ...
|2359||A fresh example - objects the Lua way|
One of the requirements we often have in our web server log file analyses is to connect together a series of accesses from a single IP address into a "visit", and this formed an example on Tuesday's Lua course.
In Lua, you can overload (i.e. redefine) operators so that they work differently on tables ...
|2318||For Lua Programmers AND for Town Planners|
I'm always happy to be able to make multiple use of the same piece of work, and so it is with an example written last week.
READ THIS SIDE ... if you are a Lua or game programmer
I was asked to provide an exercise during the Lua course that related to game playing - where a series of elements (of ...
|1819||Calling base class constructors|
In all object oriented languages, you have a facility called inheritance where you can define one type of thing ("class of object") based on another, and the newly defined class ("subclass" or "extended class") takes the initial ("base") class as it starting point.
In your code for your base class, ...
|1743||First class functions in Lua lead to powerful OO facilities|
"In Lua, function names are first class variables." Ok - so what does that mean?
At the most direct / simplest, it means that a function can be stored in a variable - so that you can write
print ("does the real work!")
action = oik
|1699||If you are learning Lua, here are some more examples|
During some courses, I write few (if any) new examples in front of my delegates, rather relying on the standard ones in the training notes (which I wrote anyway!) and making small changes and adjustments to illustrate how things work. On other courses, though, I write a some new examples from scratch ...
|1692||Towards Object Oriented Programming in Lua|
I've spent a lot of the weekend working on our Lua course ... and for those of you new to this language I'll be sharing a few snippets with you here on "The Horse's Mouth" over coming weeks. Today, I've got a short section which starts to introduce how you can do OO programming in Lua. The section ...
|Cube.lua|| Definition of a cube object|
|Dice.lua|| Definition of a dice object - Inheritance|
|Sphere.lua|| Definition of a sphere object|
|acobj|| The OO paradigm applied in Lua|
|alog|| Program to use visit objects|
|anim_o1.lua|| Tables to associate functions with data|
|animals.lua|| Using a metatable to define print, <, etc|
|cat|| adding in extra behavious|
|couples|| Moving towards Lua objects - Metatables|
|cuboid|| Illustrates use of a class of objects|
|dibdab|| We can keep funtions in a table too|
|index_123|| __index and __newindex|
|kitten|| Setting up tables and printing them out - control|
|lottaob|| Polymorphism in Lua - table of objects of different types|
|meta1|| preparations for metatables - this is just a table|
|meta2|| Simple use of a metatable to create a class|
|meta3|| creating and using a table of tables("objects")|
|meta4|| redefining operators|
|metamid|| Adding user methods via a metatable|
|metapetite|| Overriding an operator on a table|
|metasmall|| Metatable to change a table characteristics|
|metatiny|| Metatables - the mechanism|
|newCube.lua|| Definition of a class in Lua|
|north|| Comparing dot and colon |
|oblet|| Hello Object World|
|obtable|| Polymorphism in Lua - table of objects of different types|
|overload_add.lua|| Operator (+) and to_string overloading, Lua|
|rail5|| a table for each station|
|tabby|| Define special behaviours ...|
|visit.lua|| definition of visit objects|
|xlog.lua|| Access log - exercise framework|
|zoo.txt|| Sample data file for animal demo|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from http://www.training-notes.co.uk
Mutable and Immutable Values.
MetaMethods and functions as members
Metatables, __index, __add and __lt
Polymorphism and inheritance
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.