3

Scala コンパイラ (2.9.2) のヘルプ メニューには、

-print   Print program with Scala-specific features removed.

ただし、-printオプションを指定した次の呼び出しは、Scala 固有の機能を示しています。

C:\Users\John\Test Scala Project\src\main\scala>type test.scala
trait A

C:\Users\John\Test Scala Project\src\main\scala>scalac -print test.scala
[[syntax trees at end of cleanup]]// Scala source: test.scala
package <empty> {
  abstract trait A extends java.lang.Object
}

特性がまだ表示されているのはなぜですか? 純粋な Java コードを期待していたでしょう。

4

1 に答える 1

10

説明は確かにだまされていますが、Javaコードを出力するとは決して言われていません。とにかくそれはできませんでした。Scalaは有効なバイトコードを生成しますが、Javaに直接変換できるバイトコードは生成しません。

cleanupフェーズの後にコードを生成します。ここで、-Xshow-phasesScala 2.9.2を試してみると、次のように表示されます。

    phase name  id  description
    ----------  --  -----------
        parser   1  parse source into ASTs, perform simple desugaring
         namer   2  resolve names, attach symbols to named trees
packageobjects   3  load package objects
         typer   4  the meat and potatoes: type the trees
superaccessors   5  add super accessors in traits and nested classes
       pickler   6  serialize symbol tables
     refchecks   7  reference/override checking, translate nested objects
  selectiveanf   8
      liftcode   9  reify trees
  selectivecps  10
       uncurry  11  uncurry, translate function values to anonymous classes
     tailcalls  12  replace tail calls by jumps
    specialize  13  @specialized-driven class and method specialization
 explicitouter  14  this refs to outer pointers, translate patterns
       erasure  15  erase types, add interfaces for traits
      lazyvals  16  allocate bitmaps, translate lazy vals into lazified defs
    lambdalift  17  move nested functions to top level
  constructors  18  move field definitions into constructors
       flatten  19  eliminate inner classes
         mixin  20  mixin composition
       cleanup  21  platform-specific cleanups, generate reflective calls
         icode  22  generate portable intermediate code
       inliner  23  optimization: do inlining
      closelim  24  optimization: eliminate uncalled closures
           dce  25  optimization: eliminate dead code
           jvm  26  generate JVM bytecode
      terminal  27  The last phase in the compiler chain

cleanupこれが前の最後のフェーズicodeであり、それが本当に重要な点であることに注意してください。このパラメーター-printは、抽象構文木を変更するすべてのものが実行された後に、ものを出力します。ASTがコード生成の準備ができたとき。

于 2012-06-28T00:21:42.917 に答える