0__*_*0__ 3 scala nullpointerexception
这可能是由于在覆盖的博客条目由Jesse Eichar,我仍然无法弄清楚如何纠正以下不诉诸懒丘壑,使得NPE是固定的:
特定
trait FooLike { def foo: String }
case class Foo(foo: String) extends FooLike
trait Sys {
type D <: FooLike
def bar: D
}
trait Confluent extends Sys {
type D = Foo
}
trait Mixin extends Sys {
val global = bar.foo
}
Run Code Online (Sandbox Code Playgroud)
第一次尝试:
class System1 extends Mixin with Confluent {
val bar = Foo("npe")
}
new System1 // boom!!
Run Code Online (Sandbox Code Playgroud)
第二次尝试,改变mixin顺序
class System2 extends Confluent with Mixin {
val bar = Foo("npe")
}
new System2 // boom!!
Run Code Online (Sandbox Code Playgroud)
现在我同时使用bar和global非常严重,因此,我不想支付懒-VAL税只是因为Scala(2.9.2)没有得到正确的初始化.该怎么办?
kir*_*uku 11
您可以使用早期初始化程序:
class System1 extends {
val bar = Foo("npe")
} with Mixin with Confluent {
// ...
}
scala> new System1
res3: System1 = System1@1d0bfedd
Run Code Online (Sandbox Code Playgroud)