我無法在spark-shell repl中的代碼下運行。這是Scala cookbook example provided by Alvin Alexander 再現我的錯誤:火星殼給出錯誤的工廠方法示例
<console>:22: error: reference to Animal is ambiguous;
it is imported twice in the same scope by
import $VAL11.Animal
and import INSTANCE.Animal
val test = Animal("dog")
這正常使用Scala REPL。你能告訴我如何讓這個例子在spark-shell上工作嗎?
非常感謝您的幫助!
trait Animal {
def speak
}
object Animal {
private class Dog extends Animal {
override def speak = {
println("woof")
}
}
private class Cat extends Animal {
override def speak { println("meow") }
}
def apply(s: String):Animal = {
if (s == "dog") return new Dog
else return new Cat
}
}
# repl
Animal("dog")
# compiling
object test {
def main(args: Array[String]){
Animal(args(0)).speak
}
}
使用':paste'命令,這樣'動物'同伴一起編譯。這是你的問題嗎? –
謝謝@ som-snytt。我使用:粘貼。在您發表評論後,我意識到我正在使用spark-shell而不是scala repl。我會改變我的問題。 –
可能是https://issues.scala-lang.org/browse/SI-9740。如果你嘗試'動物(「帕格」)//顯示'它可能會顯示什麼是進口。 –