Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?

后端 未结 2 395
别跟我提以往
别跟我提以往 2021-01-17 16:47

As I understood call-by-name parameters of a method, the corresponding argument expression will not be evaluated when passing it to the method, but only when (a

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-17 17:40

    By-name arguments are evaluated whenever they are mentioned. The spec says that right-associative operator method calls are evaluated like this:

    a op_: b
    

    desugars to:

    { val someFreshName = a; b.op_:(someFreshName) }
    //                   ↑↑↑
    // Eval happens here ↑↑↑
    

提交回复
热议问题