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

╄→гoц情女王★ 提交于 2019-12-01 16:28:06

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 ↑↑↑

It's a bug. An old one, at that.

See SI-1980 and PR #2852.

The linked pull request added a compiler warning when using the -Xlint flag:

<console>:13: warning: by-name parameters will be evaluated eagerly when called as a right-associative infix operator. For more details, see SI-1980.
         def :: (x: =>Int) = new Node(x)  // a right-associative method
             ^
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!