Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

__index

When you try to access a nonexistent field in a table, the result is nil. However, if the table being accessed has a meta table and that meta table has an __index meta method, the meta method will be called.

There are two tables, x and y. Neither table has a hello key, but both tables try to print this key. In both instances, the print statement will result in nil:

x = {  foo = "bar"}y = { }print (x.foo) -- barprint (x.hello) -- nilprint (y.foo) -- nilprint (y.hello) -- nil

To fix this, create a meta table with an __index meta method. _index takes two arguments. The first argument is the table that is being indexed, the second argument is the key.

The following code creates two meta tables, z and w; both have an __index meta method. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content