To know what happens under the hood
scala -Xprint:typer -e "for {
a <- Some(1)
b <- Some(2)
c = a+b //Yes. This does extra map call and Tuple2 allocation .
} yield(c)"
[[syntax trees at end of typer]] // scalacmd4782374019949884812.scala
package <empty> {
object Main extends scala.AnyRef {
def <init>(): Main.type = {
Main.super.<init>();
()
};
def main(args: Array[String]): Unit = {
final class $anon extends scala.AnyRef {
def <init>(): <$anon: AnyRef> = {
$anon.super.<init>();
()
};
scala.Some.apply[Int](1).flatMap[Int](((a: Int) => scala.Some.apply[Int](2).map[(Int, Int)](((b: Int) => {
val c: Int = a.+(b);
scala.Tuple2.apply[Int, Int](b, c)
})).map[Int](((x$1: (Int, Int)) => (x$1: (Int, Int) @unchecked) match {
case (_1: Int, _2: Int)(Int, Int)((b @ _), (c @ _)) => c
}))))
};
{
new $anon();
()
}
}
}
}
scala -Xprint:typer -e "for {
a <- Some(1)
b <- Some(2)
} yield(a+b)"
[[syntax trees at end of typer]] // scalacmd6610997622100044490.scala
package <empty> {
object Main extends scala.AnyRef {
def <init>(): Main.type = {
Main.super.<init>();
()
};
def main(args: Array[String]): Unit = {
final class $anon extends scala.AnyRef {
def <init>(): <$anon: AnyRef> = {
$anon.super.<init>();
()
};
scala.Some.apply[Int](1).flatMap[Int](((a: Int) => scala.Some.apply[Int](2).map[Int](((b: Int) => a.+(b)))))
};
{
new $anon();
()
}
}
}
}