Pardon me if the title doesn't make much sense. I had to choose between Is Lua's way of comparison usefull? and Comparison in Lua.
I wanted to do something like this today:
if currChar == nextChar == "-" then
...
end
but it kept returning false
everytime:
> currChar="-"
> nextChar="-"
> =currChar == nextChar == "-"
false
>
-- All true in Python
print(5 == 5) -- true
print(5 == 5 == 5) -- false
print((5 == 5) == (5 == 5)) -- true
print(5 == (4 + 1) == (6 - 1)) -- false
I fiddled with the values for some time and found out that for some reason, Lua compares values pairwise from left to right:
> = 52 > 3 > 2
stdin:1: attempt to compare number with boolean
stack traceback:
stdin:1: in main chunk
[C]: in ?
>
I's there a case when such a form of comparison is usefull?
Why do comparisons that way?