英文:
How does lua know that the __index function's second parameter is the key?
问题
I find that Lua's object-oriented system and metatables are very confusing, as they make simple class and instance grammar more complex than that of C++. I was looking at this code (for this example https://www.runoob.com/lua/lua-metatables.html)
mytable = setmetatable({key1 = "value1"}, {
__index = function(mytable, key)
if key == "key2" then
return "metatablevalue"
else
return nil
end
end
})
print(mytable.key1, mytable.key2)
In line 2, the __index
metamethod points to a function with two parameters. The first is mytable
(itself), and the second is key
. So when the last line of print
tries to find key2
in mytable
, mytable
starts looking at the nameless metatable. However, I don't know why the second parameter is bound to the input-passing key
. It can't be because the parameter's name is key
, so Lua is smart enough to understand that the parameter is for key
because it spells "k-e-y." I don't think key
is a keyword in Lua. Are there any hidden conventions about this naming or this __index = function(arg1, arg2)
stuff?
英文:
I find that lua'a object orient system and metatable very confusing, it makes simple class and instance grammar more complex than that of the C++.
I was looking at this code
(for this example https://www.runoob.com/lua/lua-metatables.html)
mytable = setmetatable({key1 = "value1"}, {
__index = function(mytable, key)
if key == "key2" then
return "metatablevalue"
else
return nil
end
end
})
print(mytable.key1,mytable.key2)
In line2, the __index metamethod points to a function with two parameters, first is mytable(itsefl), second is key.
So when the last line of print try to find key2 in mytable, mytables starts looking at the nameless metatable. However, I don't know why the second parameter is bind the input passing key.
It can't be because the paramter's name is key so lua is so smart to understand that that parameter is for key because it spells k-e-y. I don't think key is a keyword in lua. Is there any hidden conventions about this naming or this __index=function(arg1,arg2) stuff?
答案1
得分: 2
mytable
和 key
只是参数名称(就像在常规 Lua 函数中一样),如果您希望,您可以重命名它们。
__index = function(t, k)
if k == "key2" then
return "metatablevalue"
else
return nil
end
为什么第二个参数绑定了输入的键。
这是元方法的定义。
当调用 __index
元方法时,Lua 按以下顺序传递两个参数:
参数 #1 = 要索引的对象,
参数 #2 = 索引。
英文:
mytable
and key
are just parameter names (as in usual Lua function), you can rename them if you wish.
__index = function(t, k)
if k == "key2" then
return "metatablevalue"
else
return nil
end
> why the second parameter is bind the input passing key.
It is by definition of the metamethod.
When __index
metamethod is invoked, Lua passes two arguments in the following order:
arg#1 = an object to be indexed,
arg#2 = an index.
答案2
得分: 1
检查是否不确定,使用 print(...)
lua_debug> test = setmetatable({}, {__index = function(...) print(...) return({...}) end})
lua_debug> print(test.hello) -- 触发 __index
table: 0x02a0a6549db0 hello -- print(...) 输出表对象和键
table: 0x02a0a65a99b0 -- __index 元方法函数的返回值
<details>
<summary>英文:</summary>
Check out if unsure with print(...)
lua_debug> test = setmetatable({}, {__index = function(...) print(...) return({...}) end})
lua_debug> print(test.hello) -- Triggering __index
table: 0x02a0a6549db0 hello -- print(...) gives out the table object and key
table: 0x02a0a65a99b0 -- The return of the __index Metatmethod Function
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论