July 2018
Beginner
202 pages
5h 42m
English
The __newindex meta method is complementary to the __index meta method. Where __index is used to retrieve values from missing keys in a table, __newindex is used to assign values to missing keys. The __newindex method takes three arguments:
Here is an example of using the __newindex meta method:
x = { }y = { }z = { __index = function(table, key) return z[key] end, __newindex = function(table, key, value) z[key] = value end}setmetatable(x, z)setmetatable(y, z)x.foo = "bar"print (x.foo) print (y.foo)print (z.foo)
In this example, both tables x and y have meta table z. The z meta table has an __index meta method, which searches for a key within z whenever ...
Read now
Unlock full access