If you're using Scala 2.10 or higher, you can use the following to show desugared scala code in the repl:
import scala.reflect.macros.Context // use BlackboxContext if you're in 2.11
import scala.reflect.runtime.universe._
import scala.language.experimental.macros
def _desugar(c : Context)(expr : c.Expr[Any]): c.Expr[Unit] = {
import c.universe._
println(show(expr.tree))
reify {}
}
def desugar(expr : Any): Unit = macro _desugar
This will allow you to pass in blocks of code, and see what they translate into. For your example, in the 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)))))
It's a bit messy, because the repl creates several intermediate temporary variables, but you can see the structure of what's happening.
new A().flatMap { (x: Int) =>
new A().map { (y: Int) =>
x == y
}
}
This works for most any expression, and lets you inspect what the actual code will translate to during compilation.
Update
I should point out my source - my version of desugar
is a slightly modified version of a function found in the Macrocosm repo on github.