Trending September 2023 # Working And Examples Of Lua Metatable # Suggested October 2023 # Top 14 Popular | Lanphuongmhbrtower.com

Trending September 2023 # Working And Examples Of Lua Metatable # Suggested October 2023 # Top 14 Popular

You are reading the article Working And Examples Of Lua Metatable updated in September 2023 on the website Lanphuongmhbrtower.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Working And Examples Of Lua Metatable

Introduction of Lua Metatable

Web development, programming languages, Software testing & others

Syntax:

table_whose_metatable_must_be_set = {} table_to_be_set_as_metatable = {} setmetatable = (table_whose_metatable_must_be_set, table_to_be_set_as_metatable) getmetatable(metatable)

where table_whose_metatable_must_be_set is the table for which the meta table must set up,

table_to_be_set_as_metatable is the table which will be set up as the meta table for table_whose_metatable_must_be_set table,

metatable is the meta table for a given table which can be obtained using getmetatable(metastable) method.

Working of Metatable In Lua

Working of meta table in Lua is as follows:

A table in Lua is associated with another table called meta table which helps to change the behavior of the table by making use of key set and meta methods.

The meta methods provides powerful functionalities that enables us to add or change functionalities to the operators present in the tables.

__index is one of the meta methods used to look up the meta tables when the key is not available in the table.

__newindex is one of the meta methods used to add new keys to the table if the keys are not available in the table.

__newindex meta method updates the already existing key in the table and adds the new keys only if keys are not available in the table.

The two important meta methods to handle the meta table are setmetatable(table, metable) and getmetatable(table).

The setmetatable(table, metatable) meta method is used to set the meta table for a given table.

The getmetatable(table) meta method is used to get the meta table of a given table.

Examples of Lua Metatable

Following are the examples are given below:

Example #1

Lua program to demonstrate meta tables where we create an empty table and try to look up a key in the empty table and then create a meta table and use __index meta method to look up the meta table when the key is not available in the empty table and display the result as the output on the screen:

Code:

--defining an empty table called square square = { } --trying to look up for a value in the empty table print("the value of the key in the table is:n") print(square[2]) --creating a meta table called metatablesquare for the empty table square metatablesquare = {} setmetatable( square, metatablesquare ) --defining a __index meta method in the meta table metatablesquare.__index = function(value, key) return key * key end --trying to l0ok up a value in the meta table and it accesses the meta method __index to display the result on the screen print("nthe value of the key in the meta table is:n") print(square[2])

In the above program, we are creating an empty table called square. Then we are trying to look up for a key in the table and hence the output returned is nil. Then we are creating a meta table called metatablesquare for the table square using setmetatable method within which we are defining a function using __index meta method. Then we are again accessing the table square to look up for a key and this time the value is looked up from the meta table and the result is returned which is displayed as the output on the screen. The output is shown in the snapshot above.

Example #2

Lua program to demonstrate meta tables where we create an empty table and try to look up a key in the empty table and then create a meta table and use __index meta method to look up the meta table when the key is not available in the empty table and display the result as the output on the screen:

--defining an empty table called add add = { } --trying to look up for a value in the empty table print("the value of the key in the table is:n") print(add[4]) --creating a meta table called metatablesadd for the empty table add metatableadd = {} setmetatable( add, metatableadd ) --defining a __index meta method in the meta table metatableadd.__index = function(value, key) return key + key end --trying to look up a value in the meta table and it accesses the meta method __index to display the result on the screen print("nthe value of the key in the meta table is:n") print(add[4])

Output:

In the above program, we are creating an empty table called add. Then we are trying to look up for a key in the table and hence the output returned is nil. Then we are creating a meta table called metatableadd for the table add using setmetatable method within which we are defining a function using __index meta method. Then we are again accessing the table add to look up for a key and this time the value is looked up from the meta table and the result is returned which is displayed as the output on the screen. The output is shown in the snapshot above.

Conclusion

In this article, we have learnt the concept of meta class in Lua through definition, syntax and working of meta class along with the meta methods in Lua with corresponding programming examples and their outputs to demonstrate them.

Recommended Articles

We hope that this EDUCBA information on “Lua Metatable” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

You're reading Working And Examples Of Lua Metatable

Update the detailed information about Working And Examples Of Lua Metatable on the Lanphuongmhbrtower.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!