Skip to content

Commit 18472bb

Browse files
committed
extra info added
1 parent c936d4c commit 18472bb

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

tutorials/metatables/metatables.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,38 @@ will read the value from the `LookItUpHereTable` table, and `__newindex` will se
272272
T.NewIndex = "I am new"
273273
print(WriteItBackHereTable.NewIndex)
274274

275+
276+
## Extra Info
277+
278+
In all examples I used separate tables to show which table contains the event information, but Lua has not restrictions
279+
here. We can also put the events and data in the same table.
280+
281+
T =
282+
{
283+
Foo = 123,
284+
__index = function(t,k) return 456 end,
285+
}
286+
287+
setmetatable(T, T)
288+
289+
print(T.Foo)
290+
print(T.Bar)
291+
292+
Also, Lua doesn't forbid using the *metatable* as the `__index` table.
293+
294+
T = { }
295+
296+
M =
297+
{
298+
IDontExistInT = "I exist in M"
299+
}
300+
301+
M.__index = M
302+
303+
setmetatable(T, M)
304+
305+
print(T.IDontExistInT)
306+
307+
We won't discuss it here, because there are already so many Lua OOP tutorials, but these mechanism are used for realizing
308+
an OOP like behavior in Lua. I hope that what you have learned here will help you to understand the OOP tutorials or
309+
give you ideas what you could do with these *metatables*.

0 commit comments

Comments
 (0)