Metatables: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
>Crazypotato4
so yeah it took me like a few minutes or so to rewrite this stuff and i might not have done a great job but it's probably still better than what we had before trololo
Line 20: Line 20:
===setmetatable() and getmetatable()===
===setmetatable() and getmetatable()===
The two primary functions for giving and finding a table's metatable, are setmetatable() and getmetatable().
The two primary functions for giving and finding a table's metatable, are setmetatable() and getmetatable().
<code lua>
<code lua>
local x = {}
local x = {}
Line 27: Line 28:
</code>
</code>


===rawset(), rawget(), rawequal()===
The setmetatable() function also returns the table that you're setting the metatable of, so these two scripts do the same thing:
setmetatable() is good for creating a proper metatable, but you might encounter problems.


Let's say that we want to make a table error() whenever you try to access a nil value in it.
<code lua>
<code lua>
local planets = {Earth = "COOL!"}
local x = {}
local mt = {
setmetatable(x, {})
__index = function(self, i)
return self[i] or error(i .. " is not a real Planet!")
end
}
 
setmetatable(planets, mt);
print(planets.Earth);    --> COOL!
print(planets.Pluto);    --> error: C stack overflow
</code>
</code>


So what does C stack overflow mean?
It means you've called the same function too many times inside of itself (or recursed too deeply). You called a function too many times too quickly! The normal limit is actually right around 20,000!
So what caused the __index() method to call itself?
The answer: self[i].
So what can we do? How can we get a member of a table without calling it's __index() metamethod?
With [[Function_Dump/Core_Functions#rawget_.28table.2C_index.29|rawget()]], of course!
{{EmphasisBox|[[Function_Dump/Core_Functions#rawget_.28table.2C_index.29|rawget()]], [[Function_Dump/Core_Functions#rawset_.28table.2C_index.2C_value.29|rawset()]], and [[Function_Dump/Core_Functions#rawequal_.28v1.2C_v2.29|rawequal()]] are very important when making custom metatables because they do not invoke a table's metamethods, so things like __index and __newindex aren't activated!}}
Let's make a small change to the code and try again!
<code lua>
<code lua>
local planets = {Earth = "COOL!"}
local x = setmetatable({}, {})
local mt = {
__index = function(self, i)
return rawget(self, i) or error(i .. " is not a real Planet!")
end
}
setmetatable(planets, mt)
print(planets.Earth)    --> COOL!
print(planets.Pluto)   --> error: Pluto is not a real Planet!
</code>
</code>


==Metatable Demonstration==
===Metamethods===
 
{{Example|<code lua>
local list = {1, 2}
print("In table List, key \"z\" is "..tostring(list.z)) --> In table List, key "z" is nil
</code>
 
Now, look at what happens with metatables:
 
<code lua>
local list = {1, 2}      -- A normal table
local metatable = {      -- A metatable
__index = function(t, key)
rawset(t, key, 0) -- Set t[key] to 0
return t[key]    -- return t[key] (which is now 0)
end
}
setmetatable(list, metatable) -- Now list has the metatable metatable
print("In table List, key \"z\" is "..tostring(list.z)) --> In table List, key "z" is 0
</code>}}
 
What happened there? <tt>list</tt> is a normal table, with nothing unusual about it. <tt>metatable</tt> is also a table, but it has something special: the <tt>__index</tt> metamethod. The <tt>__index</tt> metamethod is fired when <tt>t[key]</tt> is nil, or in this case, <tt>list.z</tt> is nil. Now, nothing would happen because the two tables (<tt>list</tt> and <tt>metatable</tt>) aren't linked. That's what the third line does: sets <tt>list</tt>'s metatable to <tt>metatable</tt>, which means that when <tt>list</tt> is indexed (<tt>__index</tt>) at an index that's nil (<tt>list.z</tt>), the function associated with <tt>__index</tt> in Metatable is run. The function at <tt>__index</tt> in <tt>metatable</tt> uses <tt>rawset</tt> to make a new value in the Table (or <tt>list</tt>). Then, that value is returned. So, <tt>list.z</tt> is set to 0, and then <tt>list.z</tt> is returned (which is 0).
 
==Metamethods==


Metamethods are the functions that are stored inside a metatable. They can go from
Metamethods are the functions that are stored inside a metatable. They can go from
Line 100: Line 45:
of metamethods that can be used:
of metamethods that can be used:


*__index(<var>table</var>, <var>index</var>) — Fires when an index that doesn't have a value yet is indexed.
*__index(<var>table</var>, <var>index</var>) — Fires when table[index] is indexed, where table[index] is nil.
*__newindex(<var>table</var>, <var>index</var>, <var>value</var>) — Fires when a new value is inserted to the table. Note: table.insert and table.remove do not invoke this metamethod or any other metamethod.
*__newindex(<var>table</var>, <var>index</var>, <var>value</var>) — Fires when table[index] tries to be set (table[index] = value), where table[index] is nil.
*__call(<var>table</var>, <var>...</var>) — Allows the table to be used as a function, <var>...</var> is the arguments that were passed.
*__call(<var>table</var>, <var>...</var>) — Fires when the table is called like a function, <var>...</var> is the arguments that were passed.
*__concat(<var>table</var>, <var>value</var>) — The .. concatenation operator.
*__concat(<var>table</var>, <var>value</var>) — Fires when the .. concatenation operator is used on the table.
*__unm(<var>table</var>) — The unary – operator.
*__unm(<var>table</var>) — Fires when the unary – operator is used on the table.
*__add(<var>table</var>, <var>value</var>) — The + addition operator.
*__add(<var>table</var>, <var>value</var>) — The + addition operator.
*__sub(<var>table</var>, <var>value</var>) — The – subtraction operator.
*__sub(<var>table</var>, <var>value</var>) — The – subtraction operator.
Line 111: Line 56:
*__mod(<var>table</var>, <var>value</var>) — The % modulus operator.
*__mod(<var>table</var>, <var>value</var>) — The % modulus operator.
*__pow(<var>table</var>, <var>value</var>) — The ^ exponentiation operator.
*__pow(<var>table</var>, <var>value</var>) — The ^ exponentiation operator.
*__tostring(<var></var>) — Fired when tostring is called on the table.
*__tostring(<var></var>) — Fired when [[Function_Dump/Core_Functions#tostring_.28e.29|tostring]] is called on the table.
*__metatable — if present, locks the metatable so getmetatable will return this instead of the metatable and setmetatable will error. Non-function value.
*__metatable — if present, locks the metatable so getmetatable will return this instead of the metatable and setmetatable will error. Non-function value.
*__eq(<var>table</var>, <var>value</var>) — The == equal to operator˚
*__eq(<var>table</var>, <var>value</var>) — The == equal to operator˚
*__lt(<var>table</var>, <var>value</var>) — The < less than operator˚
*__lt(<var>table</var>, <var>value</var>) — The < less than operator˚; '''NOTE:''' Using the >= greater than or equal to operator will invoke this metamethod and return the opposite of what this returns, as greater than or equal to is the same as not less than.
*__le(<var>table</var>, <var>value</var>) — The <= operator˚
*__le(<var>table</var>, <var>value</var>) — The <= operator˚; '''NOTE:''' Using the > greater than operator will invoke this metamethod and return the opposite of what this returns, as greater than is the same as not less than or equal to.
*__mode — Used in [[Weak Tables]], declaring whether the keys and/or values of a table are weak.
*__mode — Used in [[Weak Tables]], declaring whether the keys and/or values of a table are weak.
*__gc(<var>table</var>) — Fired when the table is garbage-collected.
*__gc(<var>table</var>) — Fired when the table is garbage-collected.
*__len(<var>table</var>) — Fired when the # operator is used on the Object. '''NOTE:''' Only userdatas actually respect the __len() metamethod
*__len(<var>table</var>) — Fired when the # length operator is used on the Object. '''NOTE:''' Only userdatas actually respect the __len() metamethod in Lua 5.1


˚ Requires two tables; does not work with a table and a number, string, etc. The tables must have the '''same''' metatable.
˚ Requires two values with the ''same'' metatable; does not work with a table and another random value.


==Using Metatables==
==Using Metatables==
Line 128: Line 73:
local table1 = {10,11,12}
local table1 = {10,11,12}
local metatable = {
local metatable = {
__unm = function(t) -- __unm is for the unary operator -
__unm = function(t) -- __unm is for the unary - operator
local negatedTable = {} -- the table to return
for key, value in pairs(t) do
for key, value in pairs(t) do
negatedTable[key] = -value  
t[key] = -value -- negate all of the values in this table
end
end
return negatedTable -- return the negative Table!
return t -- return the table
end
end
}
}
Line 152: Line 96:
</code>}}
</code>}}


__index was fired when x was accessed from the table. __index then defined x as 1 instead of nil; therefore, 1 was returned.
__index was fired when x was indexed in the table and not found. Lua then searched through the __index table for an index called x, and, finding one, returned that.


Now you can easily do that with a simple function, but there's a lot more where
Now you can easily do that with a simple function, but there's a lot more where
Line 197: Line 141:


local metatable = {
local metatable = {
__add = function(table1, table2)
__add = function(t1, t2)
local sum = {}
local sum = {}
for key, value in pairs(table1) do
for key, value in pairs(table1) do
Line 244: Line 188:
}, {
}, {
__call = function(a, b)  
__call = function(a, b)  
return table.concat(a, b .. ' ') .. b
return table.concat(a, b)
end
end
})
})
print('Tables contains '..t(1)) --> Tables contains 101 201 301
print('Table contains '..t(", ")) --> Table contains 10, 20, 30
</code>}}
</code>}}


Line 279: Line 223:
}
}


local setmetatable(Table, Metatable)
setmetatable(Table, Metatable)


print(Table[1]) -- Will be slow because it's the first time using this number.
print(Table[1]) -- Will be slow because it's the first time using this number, so it has to run the math function.
print(Table[2]) -- will be slow because it's the first time using this number.
print(Table[2]) -- will be slow because it's the first time using this number.
print(Table[1]) -- will be fast because it's just grabbing the number from the table.
print(Table[1]) -- will be fast because it's just grabbing the number from the table.
</code>}}
</code>}}
==Rawset, Rawget, Rawequal==
When playing with metatables, you may run into some problems.  What happens if you need to use the __index metamethod to create new values in a table, but that table's metatable also has a __newindex metamethod in it?  You'll want to use the Lua built-in function [[Function_Dump/Core_Functions#rawset_.28table.2C_index.2C_value.29|rawset]] to set the value without invoking any metamethods.  Take the following code as an example of what happens if you '''don't''' use this functions.
{{Example|<code lua>
local Table = setmetatable({}, -- the table
{
__index = function(self, i)
self[i] = i * 10 -- just as an example
return self[i]
end,
__newindex = function(self, i, v)
--don't do anything because we don't want you to set values to the table the normal way
end
})
print(Table[1]) -- Causes a C-Stack overflow
</code>}}
Now why would that cause a stack overflow?  Stack overflows happen when you try to call a function from itself too many times, but what would cause that to happen?  In the __index function, we set self[i] to a value, so when it gets to the next line, self[i] should exist, so it won't call the __index metamethod, right?
The problem is that __newindex doesn't let us set the value.  Its presence stops values from being added to the table with the standard t[i] = v method.  In order to get past this, you use the rawset function.
{{Example|<code lua>
local Table = setmetatable({}, -- the table
{
__index = function(self, i)
rawset(self, i, i * 10)
return self[i]
end,
__newindex = function(self, i, v)
--don't do anything because we don't want you to set values to the table the normal way
end
})
print(Table[1]) -- prints 10
</code>}}


==See Also==
==See Also==

Revision as of 06:21, 30 January 2012

The metatables for Strings and all ROBLOX types are locked; however, in normal Lua (not RBX.lua) you can set the metatables of these objects using the debug library.[1]

What is a Metatable?

Metatables allow tables to become more powerful than before. They are attached to data and contain values called Metamethods. Metamethods are fired when a certain action is used with the datum that it is attached to. You may think that if you have code like this:

local list = {1, 2} print(list[3])

The code will search through the list for the third index in list, realize it's not there, and return nil. That's totally wrong. What really happens is the code will search through the list for the third index, realize it's not there, and then try to see if there's a metatable attached to the table, returning nil if there isn't one.

Manipulating Metatables

setmetatable() and getmetatable()

The two primary functions for giving and finding a table's metatable, are setmetatable() and getmetatable().

local x = {} local metaTable = {} -- metaTables are tables, too! setmetatable(x, metaTable) -- Give x a metatable called metaTable! print(getmetatable(x)) --> table: [hexadecimal memory address]

The setmetatable() function also returns the table that you're setting the metatable of, so these two scripts do the same thing:

local x = {} setmetatable(x, {})

local x = setmetatable({}, {})

Metamethods

Metamethods are the functions that are stored inside a metatable. They can go from calling a table, to adding a table, to even dividing tables as well. Here's a list of metamethods that can be used:

  • __index(table, index) — Fires when table[index] is indexed, where table[index] is nil.
  • __newindex(table, index, value) — Fires when table[index] tries to be set (table[index] = value), where table[index] is nil.
  • __call(table, ...) — Fires when the table is called like a function, ... is the arguments that were passed.
  • __concat(table, value) — Fires when the .. concatenation operator is used on the table.
  • __unm(table) — Fires when the unary – operator is used on the table.
  • __add(table, value) — The + addition operator.
  • __sub(table, value) — The – subtraction operator.
  • __mul(table, value) — The * mulitplication operator.
  • __div(table, value) — The / division operator.
  • __mod(table, value) — The % modulus operator.
  • __pow(table, value) — The ^ exponentiation operator.
  • __tostring() — Fired when tostring is called on the table.
  • __metatable — if present, locks the metatable so getmetatable will return this instead of the metatable and setmetatable will error. Non-function value.
  • __eq(table, value) — The == equal to operator˚
  • __lt(table, value) — The < less than operator˚; NOTE: Using the >= greater than or equal to operator will invoke this metamethod and return the opposite of what this returns, as greater than or equal to is the same as not less than.
  • __le(table, value) — The <= operator˚; NOTE: Using the > greater than operator will invoke this metamethod and return the opposite of what this returns, as greater than is the same as not less than or equal to.
  • __mode — Used in Weak Tables, declaring whether the keys and/or values of a table are weak.
  • __gc(table) — Fired when the table is garbage-collected.
  • __len(table) — Fired when the # length operator is used on the Object. NOTE: Only userdatas actually respect the __len() metamethod in Lua 5.1

˚ Requires two values with the same metatable; does not work with a table and another random value.

Using Metatables

There are many ways to use metatables, for example the __unm metamethod (to make a table negative):

Example
{{{1}}}


Here's an interesting way to declare things using __index:

Example
{{{1}}}


__index was fired when x was indexed in the table and not found. Lua then searched through the __index table for an index called x, and, finding one, returned that.

Now you can easily do that with a simple function, but there's a lot more where that came from. Take this for example: local table = {10, 20, 30} print(table(5))

Now, obviously you can't call a table. That's just crazy, but (surprise, surprise!) with metatables you can.

Example
{{{1}}}


You can do a lot more as well, such as adding tables!

Example
{{{1}}}


If the two tables have two different __add functions, then Lua will go to table1 first and if it doesn't have an __add function, then it'll go to the second one. That means that you really only have to set the metatable of Table1 or Table2, but it's nicer and more readable to set the metatable of both.

Here is one last example breaking away from using separate variables when it isn't necessary.

Example
{{{1}}}


Use Cases

Now, I am well aware that you can do all of these as a simple function yourself, but there's a lot more than what you think it can do. Let's try a simple program that will memorize a number when a possibly laggy math problem is put into it.

For this one we will be using the __index metamethod just to make it simple:

Example
{{{1}}}


Rawset, Rawget, Rawequal

When playing with metatables, you may run into some problems. What happens if you need to use the __index metamethod to create new values in a table, but that table's metatable also has a __newindex metamethod in it? You'll want to use the Lua built-in function rawset to set the value without invoking any metamethods. Take the following code as an example of what happens if you don't use this functions.

Example
{{{1}}}


Now why would that cause a stack overflow? Stack overflows happen when you try to call a function from itself too many times, but what would cause that to happen? In the __index function, we set self[i] to a value, so when it gets to the next line, self[i] should exist, so it won't call the __index metamethod, right?

The problem is that __newindex doesn't let us set the value. Its presence stops values from being added to the table with the standard t[i] = v method. In order to get past this, you use the rawset function.

Example
{{{1}}}



See Also