Here is the following problem. When I run the following:
object Test {
def /:(s: => Unit) = {
println("/:")
s
}
}
println("A") /: Test
It prints:
A
/:
However, I was expecting it to print:
/:
A
since the last expression was supposedly rewritten Test./:(println("A"))
- which by the way, gives the second value.
Does anybody know a way to make the first syntax work, e.g. println("A") /: Test
but with call-by-name ?
Edit
Using the desugar method, I found out that the calls are desugared differently.
> desugar { println("A") /: Test}
val x$1: Unit = println("A");
Test./:(x$1)
Hence I am still wondering why this choice.
It is a known issue. If you compile with -Xlint option you should see a warning.
$ scalac -Xlint temp.scala
temp.scala:2: warning: by-name parameters will be evaluated eagerly when called
as a right-associative infix operator. For more details, see SI-1980.
def /:(s: => Unit) = {
^
one warning found
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With