If you are using Scala 2.10 or later, you can use the following to display the Scala uncooled code in repl:
import scala.reflect.macros.Context
This will allow you to go through the code blocks and see what they translate to. For your example, in repl:
scala> class A { | def getInt: Int = throw new RuntimeException | def map(f: Int => Boolean): Boolean = f(getInt) | def flatMap(f: Int => Boolean): Boolean = f(getInt) | } defined class A scala> desugar { | for { | x <- new A | y <- new A | } yield x == y | } new $line15.$read.$iw.$iw.$iw.$iw.A().flatMap(((x: Int) => new $line15.$read.$iw.$iw.$iw.$iw.A().map(((y: Int) => x.==(y)))))
This is a bit dirty because repl creates some intermediate temporary variables, but you can see the structure of what is happening.
new A().flatMap { (x: Int) => new A().map { (y: Int) => x == y } }
This works for most expressions and allows you to check what the actual code translates to at compile time.
Update
I have to point out my source - my version of desugar is a slightly modified version of the function found in the Macrocosm repository on github.
source share