Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine call-by-name colon operator and space incoherency [duplicate]

Tags:

scala

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.

like image 433
Mikaël Mayer Avatar asked Apr 06 '17 12:04

Mikaël Mayer


1 Answers

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
like image 88
Chetan Kumar Meena Avatar answered Nov 17 '22 08:11

Chetan Kumar Meena