The interesting design feature of the REPL is that your two definitions are translated to:
object A {
val greeting = "hi"
}
object B {
val greeting = "bye"
}
A subsequent usage will import the last definition:
object C {
import B.greeting
val message = s"$greeting, Bob." // your code
}
You can witness the exact wrapping strategy with scala -Xprint:parser
:
object $iw extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
import $line4.$read.$iw.$iw.greeting;
object $iw extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
val message = StringContext("", ", Bob.").s(greeting)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…