spark-shell 中的工厂方法示例给出错误
Factory method example in spark-shell giving error
我无法在 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
}
}
正如 som-snytt 所指出的,这很可能 a version of an existing scala bug.
Animal("dog") // show
结果如下:
import $line15.$read.INSTANCE.$iw.$iw.Animal;
val $line15$read = $line15.$read.INSTANCE;
import $line15$read.$iw.$iw.Animal;
请注意,Animal
被导入了两次。要解决此问题,您可以将代码包装在 object
:
中
object test{
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
}
}
}
现在,当您调用 test.Animal("dog") // show
时,您得到的是:
val $line15$read = $line15.$read.INSTANCE;
import $line15$read.$iw.$iw.$iw.$iw.$iw.$iw.$iw.$iw.test;
请注意,您只有一个更简洁的导入。
作为一个有趣的旁注,如果您 运行 您的第一个代码然后是第二个,那么 test.Animal("dog")
结果如下:
import $line15.$read.INSTANCE.$iw.$iw.Animal;
val $line16$read = $line16.$read.INSTANCE;
import $line16$read.$iw.$iw.$iw.$iw.$iw.$iw.$iw.$iw.test;
保留 Animal
导入的位置。
我无法在 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
}
}
正如 som-snytt 所指出的,这很可能 a version of an existing scala bug.
Animal("dog") // show
结果如下:
import $line15.$read.INSTANCE.$iw.$iw.Animal;
val $line15$read = $line15.$read.INSTANCE;
import $line15$read.$iw.$iw.Animal;
请注意,Animal
被导入了两次。要解决此问题,您可以将代码包装在 object
:
object test{
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
}
}
}
现在,当您调用 test.Animal("dog") // show
时,您得到的是:
val $line15$read = $line15.$read.INSTANCE;
import $line15$read.$iw.$iw.$iw.$iw.$iw.$iw.$iw.$iw.test;
请注意,您只有一个更简洁的导入。
作为一个有趣的旁注,如果您 运行 您的第一个代码然后是第二个,那么 test.Animal("dog")
结果如下:
import $line15.$read.INSTANCE.$iw.$iw.Animal;
val $line16$read = $line16.$read.INSTANCE;
import $line16$read.$iw.$iw.$iw.$iw.$iw.$iw.$iw.$iw.test;
保留 Animal
导入的位置。