当初始化类的实例时,使用随播对象在REPL(spark-shell 2.1.0)中抛出模糊引用错误(1.x.x中的问题4165已关闭)。 我在Spark 2.1.0中看到了这个错误(见下文)。
scala> :pas
// Entering paste mode (ctrl-D to finish)
class X {
var m : Long =0
def add(x: Long): X = {
m += x
this
}
}
object X {
def apply(x:Long) = new X().add(x)
}
// Exiting paste mode, now interpreting.
defined class X
defined object X
scala> val v = X(2)
<console>:26: error: reference to X is ambiguous;
it is imported twice in the same scope by
import $line14$read.X
and import INSTANCE.X
val v = X(2)
^
我错过了什么吗? 感谢
答案 0 :(得分:2)
您无法在shell中定义这样的伴随对象。你需要一个对象包装器
object myWrapper {
class X {
var m: Long = 0
def add(x: Long): X = {
m += x
this
}
}
object X {
def apply(x:Long) = new X().add(x)
}
}
import myWrapper._