为什么表达式(true == true == true)会产生语法错误? [英] Why does the expression (true == true == true) produce a syntax error?
问题描述
Ruby :
true == true == true
语法错误,意外的tEQ
vs. JavaScript :
true == true == true
// => true
vs. C :
1 == 1 == 1
// => 1
与===
,=~
和<=>
方法(所有方法都具有相同的优先级,并且排他地形成一个单独的优先级组).
因此,在上述列表中有多个运算符排成一行的情况下,评估顺序应通过任一方法明确设置
-
括号
()
:(true == true) == true # => true true == (true == true) # => true
-
或点运算符
.
(对于连续的最后一次相等检查,可以省略 ):true .== true == true # => true
Ruby:
true == true == true
syntax error, unexpected tEQ
vs. JavaScript:
true == true == true
// => true
vs. C:
1 == 1 == 1
// => 1
Association direction, which controls the order of operators having their arguments evaluated, is not defined for the ==
method, same as for ===
, !=
, =~
and <=>
methods as well (all of which have the same precedence and form a separate precedence group exclusively).
Thus evaluation order in case of multiple operators from the list mentioned above being chained in a row should be set explicitly via either
parenthesis
()
:(true == true) == true # => true true == (true == true) # => true
or dot operator
.
(can be omitted for the last equality check in a row):true .== true == true # => true
这篇关于为什么表达式(true == true == true)会产生语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!