lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm a little confused abut the changes to .__eq in Lua 5.3. Now that Lua will
call the first .__eq found if *either* of the two operands to == has a defined
.__eq method, some odd results seem to follow. In particular, == is now order
dependent. x == y and y == x can return different results. Is this really

Consider the code below and the results it would yield for Lua 5.3 versus for
earlier versions of Lua:

    foo, bar = {4}, {4}
    mt1, mt2 = {}, {}
    happy = function () return true end
    sad = function () return false end
    mt1.__eq = happy
    mt2.__eq = sad
    print(foo == bar, bar == foo) -- false, false for all versions
    setmetatable(foo, mt1)
    print(foo == bar, bar == foo) -- false, false or true, true?
    setmetatable(bar, mt2)
    print(foo == bar, bar == foo) -- false, false or true, false?

For what it's worth, this already has some real-world implications. A number of
testing libraries (and probably others) provide methods to test whether two
tables or objects are "the same". I found out about the changes to Lua while
discussing what I thought was a bug in such a testing library:

Thanks, Peter
We have not been faced with the need to satisfy someone else's
requirements, and for this freedom we are grateful.
    Dennis Ritchie and Ken Thompson, The UNIX Time-Sharing System