這是一個非常快速和骯髒的方式,以在REPL中發生的事情。
調用REPL使用Scala -Xprint:打字員
scala> class Dinner {
| val veggie="broccoli"
| def announceDinner(veggie: String){
| println("Dinner happens to be tasteless " + veggie + " soup")
| }
| }
[[syntax trees at end of typer]]// Scala source: <console>
package $line1 {
final object $read extends java.lang.Object with ScalaObject {
def this(): object $line1.$read = {
$read.super.this();
()
};
final object $iw extends java.lang.Object with ScalaObject {
def this(): object $line1.$read.$iw = {
$iw.super.this();
()
};
final object $iw extends java.lang.Object with ScalaObject {
def this(): object $line1.$read.$iw.$iw = {
$iw.super.this();
()
};
class Dinner extends java.lang.Object with ScalaObject {
def this(): $line1.$read.$iw.$iw.Dinner = {
Dinner.super.this();
()
};
private[this] val veggie: java.lang.String = "broccoli";
<stable> <accessor> def veggie: java.lang.String = Dinner.this.veggie;
def announceDinner(veggie: String): Unit = scala.this.Predef.println("Dinner happens to be tasteless ".+(veggie).+(" soup"))
}
}
}
}
}
[[syntax trees at end of typer]]// Scala source: <console>
package $line1 {
final object $eval extends java.lang.Object with ScalaObject {
def this(): object $line1.$eval = {
$eval.super.this();
()
};
private[this] val $print: String = {
$read.$iw.$iw;
"defined class Dinner\012"
};
<stable> <accessor> def $print: String = $eval.this.$print
}
}
defined class Dinner
正如你可以檢查上述Dinner
結束了包裹成$line1.$read.$iw.$iw
。現在讓我們看看接下來會發生什麼:
[[syntax trees at end of typer]]// Scala source: <console>
package $line2 {
final object $read extends java.lang.Object with ScalaObject {
def this(): object $line2.$read = {
$read.super.this();
()
};
final object $iw extends java.lang.Object with ScalaObject {
def this(): object $line2.$read.$iw = {
$iw.super.this();
()
};
import $line1.$read.$iw.$iw.Dinner;
final object $iw extends java.lang.Object with ScalaObject {
def this(): object $line2.$read.$iw.$iw = {
$iw.super.this();
()
};
private[this] val res0: $line1.$read.$iw.$iw.Dinner = new $line1.$read.$iw.$iw.Dinner();
<stable> <accessor> def res0: $line1.$read.$iw.$iw.Dinner = $iw.this.res0
}
}
}
}
[[syntax trees at end of typer]]// Scala source: <console>
package $line2 {
final object $eval extends java.lang.Object with ScalaObject {
def this(): object $line2.$eval = {
$eval.super.this();
()
};
lazy private[this] var $result: $line1.$read.$iw.$iw.Dinner = {
$eval.this.$print;
$line2.$read.$iw.$iw.res0
};
private[this] val $print: String = {
$read.$iw.$iw;
"res0: $line1.$read.$iw.$iw.Dinner = ".+(scala.runtime.ScalaRunTime.replStringOf($line2.$read.$iw.$iw.res0, 1000))
};
<stable> <accessor> def $print: String = $eval.this.$print
}
}
基本上同樣的事情之前,但使用$line2
代替$line1
。請注意0之前的import $line1.$read.$iw.$iw.Dinner
。
這樣我們可以看到爲什麼在兩個不同的行中定義伴隨對象不起作用,它們最終會被包裝到不同的對象中,同伴需要在同一範圍/源文件中定義。
[scala runtime/repl運行.scala程序時真的發生了什麼?](http://stackoverflow.com/questions/7655165/what-really-happens-behind-the-scala-runtime -repl-when-running-a-scala-program) – sschaef 2012-07-18 20:01:04
我會爲我真正想要理解的東西增加更多的清晰度。我試圖找出當它被內部封裝到一個'對象'時,類定義是否被保留。也許這並不重要,因爲我可以在類上操作並調用它的方法?我之前看過那篇文章,但並不認爲這是我的問題的確切答案,我最終發佈了我的問題。 – ilango 2012-07-18 21:32:54
你能否用一個小例子更新你的問題?我不明白你想看到什麼。 – sschaef 2012-07-18 21:42:36