Why do most programming languages only have binary equality comparison operators?

前端 未结 24 1015
一个人的身影
一个人的身影 2020-12-08 14:20

In natural languages, we would say \"some color is a primary color if the color is red, blue, or yellow.\"

In every programming language I\'ve seen, that translates

24条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 14:51

    The question is reasonable, and I wouldn't regard the change as syntactic sugar. If the value being compared is the result of computation, it would be nicer to say:

      if (someComplicatedExpression ?== 1 : 2 : 3 : 5)
    

    than to say

      int temp;
      temp = someComplicatedExpression;
      if (temp == 1 || temp == 2 || temp == 3 || temp == 5)
    

    particularly if there was no other need for the temp variable in question. A modern compiler could probably recognize the short useful lifetime of 'temp' and optimize it to a register, and could probably recognize the "see if variable is one of certain constants" pattern, but there'd be no harm in allowing a programmer to save the compiler the trouble. The indicated syntax wouldn't compile on any existing compiler, but I don't think it would be any more ambiguous than (a+b >> c+d) whose behavior is defined in the language spec.

    As to why nobody's done that, I don't know.

提交回复
热议问题