Understanding infix method call and cons operator(::) in Scala

后端 未结 2 2023
予麋鹿
予麋鹿 2021-01-31 10:30

I\'m quite new to Scala programming language, and was trying something out stucked in my mind while I was following the lecture notes at here.

I think I couldn\'t really

相关标签:
2条回答
  • 2021-01-31 11:10

    It's about precedence not execution order. + has higher precedence than ::, so a + b :: c parses as (a + b) :: c. However infix method calls with regular names have lower precedence, so a foo b c parses as a foo (b c).

    See this question for a list of operators ordered by their precedence in scala.

    0 讨论(0)
  • 2021-01-31 11:18

    There are two things of concern here: precedence and fixity. As sepp2k mentioned, this question on Stack Overflow explains the precedence, thought the rules, as quoted, are not complete enough, and there were very small changes from Scala 2.7 to Scala 2.8. Differences concern mostly operators ending in =, though.

    As for fixity, almost everything in Scala is read left to right, which is what programmers are used to. In Scala, however, operators ending in : are read right to left.

    Take, then, this example:

    1 + 2 :: Nil
    

    First, precedence. What has most precedence, + or :? According to the table, + has precedence over :, so the addition is done first. Therefore, the expression is equal to this:

    ((1).+(2)) :: Nil
    

    Now there's no precedence conflict, but since :: ends in :, it has a diferent fixity. It is read right to left, therefore:

    Nil.::((1).+(2))
    

    On the other hand, in this:

    gen nextInt 3 :: Nil
    

    The operator :: has precedence over nextInt, because : has precedence over all letters. Therefore, and remembering its fixity, it becomes:

    gen nextInt Nil.::(3)
    

    Which then becomes

    gen.nextInt(Nil.::(3))
    

    At which point the error is obvious.

    PS: I'm writing (1).+(2) instead of 1.+(2) because, at the time of this writing, 1. is interpreted as a double number, making 1.+(2) an infix expression adding the double 1.0 to 2. This syntax is deprecated as of Scala 2.10.0, and will probably not be present on Scala 2.11.

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