Reputation: 1209
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 (and if) the value of the parameter is used in the method body.
In the following example, however, this is only true in the first two method calls, but not in the third one, although it should be a merely syntactical variation of the second case!?
Why is the argument expression evaluated in the third method call?
(I tested this code using Scala 2.11.7)
class Node(x: => Int)
class Foo {
def :: (x: =>Int) = new Node(x) // a right-associative method
def !! (x: =>Int) = new Node(x) // a left-associative method
}
// Infix method call will not evaluate a call-by-name parameter:
val node = (new Foo) !! {println(1); 1}
println("Nothing evaluated up to here")
// Right-associative method call will not evaluate a call-by-name parameter:
val node1 = (new Foo).::({println(1); 1})
println("Nothing evaluated up to here")
// Infix and right-associative method call will evaluate a call-by-name parameter - why??
val node2 = {println(1); 1} ::(new Foo) // prints 1
println("1 has been evaluated now - why??")
Edit of 2020: Note that Scala 2.13 no longer shows this irritating behavior: val node2 = ...
no longer prints anything.
Upvotes: 11
Views: 878
Reputation: 369428
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 ↑↑↑
Upvotes: 3
Reputation: 55569
It's a bug. An old one, at that.
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
^
Upvotes: 10