Lua Metatables - Lua (programming language)

What is the use of Metatables?

Metatable in Lua is a table which helps in modifying the behavior of a table to which it is attached, by using a key set and related meta methods. These meta methods are powerful Lua functionality which enable features like,

  • Changing/adding functionalities to operators on tables.
  • Looking up for metatables when the key is not available in the table using __index in metatable.

Below are the two important methods used in handling metatables

  • setmetatable(table,metatable) − used to set metatable for a table.
  • getmetatable(table) − used to get metatable of a table.

Let's first look at how to set one table as metatable of another. It is shown below.

Above code can be represented in a single line as shown below.

_index

Below is a simple example of metatable for looking up the meta table when it's not available in table is shown below.

When you run the above program, below output will be displayed.

Explanation for above example is shown below in steps.

  • Table mytable here is {key1 = "value1"}.
  • Metatable is set for mytable which consists of a function for __index, which is called as metamethod.
  • metamethod does a simple job of looking up for an index "key2", if it's found, it returns "metatablevalue", otherwise returns mytable's value for corresponding index.

Simplified version of the above program is shown below.

__newindex

When we add __newindex to metatable when keys are not available in the table, behavior of new keys will be defined by meta methods. Below is a simple example where metatable's index is set when index is not available in the main table.

When you run the above program, below output will be displayed.

In the above program you observe that, if a key exists in the main table, it will just update it else, it will add that key to the metatable.

Another example which updates the same table using rawset function is shown below.

When you run the above program, below output will be displayed.

rawset sets value without using __newindex of metatable. Similarly there is rawget that gets value without using __index.

Adding Operator Behavior to Tables

A simple example to combine two tables using + operator is shown below

When you run the above program, below output will be displayed.

The __add key is included in the metatable to add behavior of operator +. Table of keys and corresponding operator is shown below.

Mode

Description

__add

This will change the behavior of operator '+'.

__sub

This will change the behavior of operator '-'.

__mul

This will change the behavior of operator '*'.

__div

This will change the behavior of operator '/'.

__mod

This will change the behavior of operator '%'.

__unm

This will change the behavior of operator '-'.

__concat

This will change the behavior of operator '..'.

__eq

This will change the behavior of operator '=='.

__lt

This will change the behavior of operator '<'.

__le

This will change the behavior of operator '<='.

__call

Adding behavior of method call is done using __call statement. A simple example which returns the sum of values in main table with the passed table is shown below.

When you run the above program, below output will be displayed.

__tostring

For changing the behavior of the print statement, you can use the __tostring metamethod. A simple example is shown below.

When you run the above program, below output will be displayed.

If you are aware of the capabilities of meta table fully, then you can really perform many operations which are very complex without using it. So, try working more on using metatables with different options which are available in meta tables as explained in the samples and also you can create your own samples.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Lua (programming language) Topics