Operator precedence of unary operators

后端 未结 1 1748
情话喂你
情话喂你 2021-02-15 10:39

Some information source on operator precedence like this says that unary operators like !, ~, +, - have higher

1条回答
  •  日久生厌
    2021-02-15 11:19

    My programming ruby book (2nd edition) also lists unary operators as having higher precedence than assignment.

    The unary operator IS being given highest precedence. The reason the line is parsed as ~ (a = 1) is because decomposing the line into valid syntax is of higher precedence than anything else, including using the simple variable 'a' as the expression the unary operator operates on.

    If the ruby parser could have made something valid of the rest of the line, it would have used (~ a), but there is no valid rule than matches = something, only lvalue '=' rvalue.

    You can regard "valid syntax" as the top priority, then simple values, constant and variable names and then the standard operators under that.

    0 讨论(0)
提交回复
热议问题