SHiLLySiT
SHiLLySiT

Reputation: 183

Why does setting the value of nested table set the metatable's table?

I'm learning lua and just discovered a quirk to metatables:

local mt = { 
  value = 0,
  nested = { value = 0 },
}
mt.__index = mt

local t1 = {}
setmetatable(t1, mt)

local t2 = {}
setmetatable(t2, mt)

print(t1.value)         -- 0
print(t1.nested.value)  -- 0
print(t2.value)         -- 0
print(t2.nested.value)  -- 0
print(mt.value)         -- 0
print(mt.nested.value)  -- 0

t1.value = 10
t1.nested.value = 20;

print(t1.value)         -- 10
print(t1.nested.value)  -- 20
print(t2.value)         -- 0
print(t2.nested.value)  -- 20 (?)
print(mt.value)         -- 0
print(mt.nested.value)  -- 20 (?)

Note the two comments with (?).

If I'm understanding this correctly, what's happening is because t1 does not have a key for nested it returns mt's nested table. So doing t1.nested.value=20 effectively sets the value in the mt and thus all tables that use it as a metatable.

I have two questions:

  1. Is my understanding correct? If not can someone explain what is happening here?
  2. Regardless of what is happening, how do I set only a specific table's nested table without affecting the metatable?

Upvotes: 1

Views: 236

Answers (2)

koyaanisqatsi
koyaanisqatsi

Reputation: 2793

  1. Is correct.
    Therefore you set the value in the metatable that are now holding a new value.

  2. Set nested first before you set nested.value.
    Like you do with value.
    And destroy it to get back the metatable nested table including value.

I suggest doing more in the interactive Lua interpreter to see what is happen...

$ lua
Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
> code=[[do
>> local mt = { 
>>   value = 0,
>>   nested = { value = 0 },
>> }
>> mt.__index = mt
>> 
>> local t1 = {}
>> setmetatable(t1, mt)
>> 
>> local t2 = {}
>> setmetatable(t2, mt)
>> dump(t1); dump(t2);
>> print(t1.value)         -- 0
>> print(t1.nested.value)  -- 0
>> print(t2.value)         -- 0
>> print(t2.nested.value)  -- 0
>> print(mt.value)         -- 0
>> print(mt.nested.value)  -- 0
>> 
>> t1.value = 10
>> t1.nested.value = 20;
>> dump(t1); dump(t2);
>> print(t1.value)         -- 10
>> print(t1.nested.value)  -- 20
>> print(t2.value)         -- 0
>> print(t2.nested.value)  -- 20 (?)
>> print(mt.value)         -- 0
>> print(mt.nested.value)  -- 20 (?)
>> end]]
> dump=require('dump') load(code)()
0
0
0
0
0
0
1 Index key: value (string) equals to 10 (number) integer number
10
20
0
20
0
20

OK you need my dump.lua...

return function(...)
local args={...}
local counter=0
-- Next patches function to work also as/for __call metamethod
-- (First argument to __call always is self, so an argument to __call is always second)
if #args == 2 then args[0]=args[1] args[1]=args[2] table.remove(args) end

-- Loop
for k,v in pairs(args[1]) do
counter=counter+1 -- For counting table keys
-- Put a funny colored out what is in the table
io.stdout:write(tostring(counter)..' Index key: \x1b[1;'..tostring(math.random(31,34))..'m',
tostring(k),
'\x1b[0m (',
type(k),
')',
' equals to \x1b[1;'..tostring(math.random(31,34))..'m',
tostring(v),
'\x1b[0m (',
type(v),
')'):flush()

-- Add some info depending on datatype (except userdata)
if type(v)=='string' then io.stdout:write(' '..tostring(#v)..' char(s)'):flush() end
if type(v)=='number' then io.stdout:write(' '..tostring(math.type(v))..' number'):flush() end
if type(v)=='table' then io.stdout:write(' '..tostring(#v)..' numbered keys in sequence'):flush() end
if type(v)=='function' then io.stdout:write(' '..tostring(debug.getinfo(v).source)..' source'):flush() end
io.stdout:write('\n'):flush() -- And add a final newline
end
-- return 'Not used'
end

PS: I love to use __index for stuff that are not visible but use and reachable
Normally it should hold functions like the datatype string shows.
Look...

> dump(getmetatable(_VERSION).__index)
1 Index key: unpack (string) equals to function: 0x565a3530 (function) =[C] source
2 Index key: pack (string) equals to function: 0x565a3950 (function) =[C] source
3 Index key: find (string) equals to function: 0x565a4db0 (function) =[C] source
4 Index key: gsub (string) equals to function: 0x565a4dc0 (function) =[C] source
5 Index key: byte (string) equals to function: 0x565a3f20 (function) =[C] source
6 Index key: len (string) equals to function: 0x565a1750 (function) =[C] source
7 Index key: dump (string) equals to function: 0x565a2d00 (function) =[C] source
8 Index key: sub (string) equals to function: 0x565a4210 (function) =[C] source
9 Index key: char (string) equals to function: 0x565a2060 (function) =[C] source
10 Index key: rep (string) equals to function: 0x565a1be0 (function) =[C] source
11 Index key: gmatch (string) equals to function: 0x565a40d0 (function) =[C] source
12 Index key: upper (string) equals to function: 0x565a1ac0 (function) =[C] source
13 Index key: reverse (string) equals to function: 0x565a1b50 (function) =[C] source
14 Index key: packsize (string) equals to function: 0x565a3420 (function) =[C] source
15 Index key: match (string) equals to function: 0x565a4da0 (function) =[C] source
16 Index key: lower (string) equals to function: 0x565a1d90 (function) =[C] source
17 Index key: format (string) equals to function: 0x565a22d0 (function) =[C] source

...that are methods for string...

print(_VERSION:rep(100,', '):upper():reverse())

( Maybe number becomes math methods in Lua 6.0 ;-) )

But with a table you can do definitely what you want.

Upvotes: 2

Luke100000
Luke100000

Reputation: 1539

Yes, 1) is correct.

For 2) the easiest way is to create a new table in t1, which again has a metatable with an __index field.

t1.nested = setmetatable({ }, {__index = t1.nested})

print(t1.nested.value)  -- 20
t1.nested.value = 30
print(t1.nested.value)  -- 30
print(mt.nested.value)  -- 20

Upvotes: 1

Related Questions