我如何看到什么[Java / Scala?]代码Scala编译器重写原始的Scala代码 [英] How can I see in what [Java/Scala?] code does Scala compiler rewrites original Scala-code
问题描述
在Scala邮件列表之后,不同的人经常说:编译器将此[scala]代码重写为此[java / scala ??]代码。例如,从最新的一个线程,如果Scala看到
类C(i:Int = 4){... }
然后编译器将其重写为(有效):
class C(i:Int){...}
对象C {
def init $ default $ 1:Int = 4
}
我如何知道,我的代码的编译器输出是什么?
您可以使用-print作为编译器选项,并且scalac将删除所有
<$ c $ c> class Main
{
def test(x:Any)= x match {
caseHello=> println(Hello World)
case e:String => println(String)
case i:Int => println(Int)
case _ => println(Something other)
}
}
scalac -print来编译它,你会得到以下Scala代码。
[[ ] // Scala source:Test.scala
package< empty> {
class Main extends java.lang.Object with ScalaObject {
def test(x:java.lang.Object):Unit = {
< synthetic> val temp1:java.lang.Object = x;
if(temp1。==(Hello))
{
scala.this.Predef.println(Hello World)
}
else
if(temp1。$ isInstanceOf [java.lang.String]())
{
scala.this.Predef.println(String)
}
else
if(temp1。$ isInstanceOf [Int]())
{
scala.this.Predef.println(Int)
}
else
{
scala.this.Predef.println(Something other)
}
};
def this():Main = {
Main.super.this();
()
}
}
}
Following Scala mailing lists, different people often say: "compiler rewrites this [scala] code into this [java/scala??] code". For example, from one of the latest threads, if Scala sees
class C(i: Int = 4) { ... }
then the compiler rewrites this as (effectively):
class C(i: Int) { ... }
object C {
def init$default$1: Int = 4
}
How can I find out, what will be the compiler output for my code? Should I decompile the resulting bytecode for that?
You can use "-print" as compiler option, and scalac will remove all Scala-specific features.
For example, here is the original code:
class Main
{
def test (x: Any) = x match {
case "Hello" => println ("Hello World")
case e: String => println ("String")
case i: Int => println ("Int")
case _ => println ("Something else")
}
}
And if you use "scalac -print" to compile it, you will get the following Scala code.
[[syntax trees at end of cleanup]]// Scala source: Test.scala
package <empty> {
class Main extends java.lang.Object with ScalaObject {
def test(x: java.lang.Object): Unit = {
<synthetic> val temp1: java.lang.Object = x;
if (temp1.==("Hello"))
{
scala.this.Predef.println("Hello World")
}
else
if (temp1.$isInstanceOf[java.lang.String]())
{
scala.this.Predef.println("String")
}
else
if (temp1.$isInstanceOf[Int]())
{
scala.this.Predef.println("Int")
}
else
{
scala.this.Predef.println("Something else")
}
};
def this(): Main = {
Main.super.this();
()
}
}
}
这篇关于我如何看到什么[Java / Scala?]代码Scala编译器重写原始的Scala代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!