Let's see the code below:
import scala.language.implicitConversions
class Foo
implicit def int2Foo(a: => Int): Foo = new Foo
def bar(foo: Foo) = {}
def bar(foo: Boolean) = {}
bar {
println("Hello")
64
}
This code does not print anything, because the block contains println("Hello")
treated as => Int
and it is converted to Foo
by int2Foo
. But the surprising thing is happen if we omit the overloaded function bar(foo: Boolean)
import scala.language.implicitConversions
class Foo
implicit def int2Foo(a: => Int): Foo = new Foo
def bar(foo: Foo) = {}
bar {
println("Hello")
64
}
This prints Hello
because it evaluates the block, and only the last statement, 64
in this case, is treated as a call-by-name parameter. I cannot understand what kind of rationale exists behind of this difference.
{...}:Int
also changes the behavior. – Westleigh