相等元方法的执行方式似乎发生了变化,但我找不到任何 5.1 中的文档,5.2或 5.3引用手册(在“不兼容”下的“语言更改”部分下)
t1a = {}
t1b = {}
t2 = {}
mt1 = { __eq = function( o1, o2 ) return 'whee' end }
mt2 = { __eq = function( o1, o2 ) return 'whee' end }
setmetatable( t1a, mt1 )
setmetatable( t1b, mt1 )
setmetatable( t2, mt2 )
print( t1a == t1b ) --> true
print( t1a == t2 ) --> false
来自 ( http://lua-users.org/wiki/MetatableEvents ) 并在本地 5.1.5 实现上测试为真
在 Lua 5.3 中运行这个实际上产生
true
true
从 ( https://www.lua.org/demo.html) 执行
这是一个错误吗?用于相等的元方法是否不再必须是相同的引用?
最佳答案
我不是 Lua 专家,但比较 5.2 docs :
"eq": the == operation. The function getequalhandler defines how Lua chooses a metamethod for equality. A metamethod is selected only when both values being compared have the same type and the same metamethod for the selected operation, and the values are either tables or full userdata.
和 5.3 docs :
__eq: the equal (==) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are either both tables or both full userdata and they are not primitively equal. The result of the call is always converted to a boolean.
5.3 文档不要求操作数具有相同的元方法。
关于lua - 平等元方法是什么时候改变的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49247299/