我有一个示例代码:
class A(str: String) {
println(s"InsideCase:::$str")
}
object A {
def apply(str: String) = {
println("foobar::")
new A(str)
}
}
object b extends App {
A("kool")
}
此代码在 Intellij 中运行良好。并给我输出:
foobar::
InsideCAse:::kool
但是当尝试在 REPL 上执行此操作时:
scala> :paste
// Entering paste mode (ctrl-D to finish)
class A(str: String) {
println(s"InsideCAse:::$str")
}
object A {
def apply(str: String) = {
println("foobar::")
new A(str)
}
}
// Exiting paste mode, now interpreting.
defined class A
defined object A
scala> A("kool")
它给了我以下错误:
<console>:27: error: reference to A is ambiguous;
it is imported twice in the same scope by
import $line31$read.A
and import INSTANCE.A
A("kool")
我在这里想念什么?如果可以,请详细解释,这将很有帮助。