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