What are meta tables and what are some examples to use them?

For a while now I have been getting by just fine with regular tables, but just recently I found out about meta tables. I did some research and watched some videos but nothing makes sense.

What are MetaTables? And what are some good examples to implement them?

1 Like
My explanation

As simple as I can put it, they are tables with very specific index names that correlate to a value
(metamethods) which can be a table or function or nothing that are attributed to whenever you do a specific action like indexing or setting a new index. These indexes are always starting with two underscores, __.

Two most common example metamethods are __index and __newindex. __index is for when you read from a table for indices that do not exist in the table attached to the metatable. __newindex is for creating a new value, it will not count if you replace an existing value however.

Essentially with __index and __newindex, they’re correlated to unexisting values. You connect them to a function or table (for __index, a function will return what would be seen if they read the table for that nil index, a table would just redirect to itself, __newindex would also redirect if you use a table but the function does not need to return).

That is the basic overview of metatables and the two popularly used metamethods. __tostring for example would occur when you attempt to use tostring function on the table.


Prominent examples to use in
  • OOP in Lua-style (Object-oriented programming)
  • Metatable where the original table is read and edited like normal but you record whatever happens, maybe like event triggers, example of that here in the advanced section. This is called a middleman or proxy table.

Did you take the time to search this up? There are so many people that already have asked this question and created many posts about metatables.

Wiki link

Example:

2 Likes

meta tables are basically used for meta-programng which is very powerful

Simplest way to understand metatables: Allows you to have events, but for tables! The metamethods displayed in the developers hub are all events you can use to make your tables more powerful.

One of them (and probably the most used one) is the __index metamethod. It fires when you index a table with a nil value. Like I said before, it’s an event, but for tables.

Probably one of the biggest questions people ask whenever they encounter this treasure is “why?” One of the most popular uses for them is OOP.

A good in depth explanation which has a pretty good application is this thread right here:

1 Like

Another remark: you aren’t forced to use only the special keys (such as __index) in a metatable. A metatable is a table like any other, except it’s closely associated with another table and can be gotten by using getmetatable on that table.

Metatables also aren’t the only way to do most of the things they’re used for.
__index can be replaced with something like this:

local index
index = function(tbl, key)
	if tbl[key] then
		return tbl[key]
	elseif tbl.parent == nil then
		return nil
	else
		return index(tbl.parent, key) -- recursive, very dangerous
	end
end

local tbl = {
	parent = {
		parent = {
			parent = {
				foo = 123
			}
		}
	}
}

print(index(tbl, "foo")) --> 123
print(tbl.foo) --> nil

The difference is that metatables let you make that happen completely transparently. Metatables can make tbl.foo return 123.

1 Like