6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Scalaのfor内包表記の変換結果を確認する

Last updated at Posted at 2021-12-15

はじめに

Scalaの初心者の人にforの挙動を説明したり、パフォーマンスの調査等をする際に、
forがどのように変換されるかチェックしたいケースがあるかと思います。

そのような時のために、
Scalaのfor内包表記の変換結果を確認する方法についてまとめました。

結論

$ scalac -Xprint:parser 対象のファイル

で確認できます。

サンプルコード

Test.scala
object Test {
  for {
    num1 <- List(1, 2, 3)
    num2 <- List(10, 20, 30)
    if num2 != 2
  } yield (num1, num2)
}

変換結果:

$ scalac -Xprint:parser Test.scala
[[syntax trees at end of                    parser]] // Test.scala
package <empty> {
  object Test extends scala.AnyRef {
    def <init>() = {
      super.<init>();
      ()
    };
    List(1, 2, 3).flatMap(((num1) => List(10, 20, 30).withFilter(((num2) => num2.$bang$eq(2))).map(((num2) => scala.Tuple2(num1, num2)))))
  }
}

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?