lua - 平等元方法是什么时候改变的?

标签 lua

相等元方法的执行方式似乎发生了变化,但我找不到任何 5.1 中的文档,5.25.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/

相关文章:

lua - 如何在.lua中重命名数组中的字段

lua - 如何使用/包含 lpeg luapeg re 模块

random - Lua中获取随机数的其他方法

function - 关于lua for循环

hash - CRC32 整数散列到字符串

Lua 模式匹配

mysql - Lua 和 SQL 语法错误

lua - 创建递归 LPeg 模式

lua - 如何强制执行 Lua 脚本运行时限制?

function - Lua - 检查用户输入的表是否存在并从中读取