Consider the following (simplified) example:
abstract class Bar[T] {
val f: PartialFunction[T, T]
val default: PartialFunction[T, T] = { case x => x }
val chained = f orElse default
}
class Foo extends Bar[Int] {
val f: PartialFunction[Int, Int] = { case 1 => 2 }
}
And watch it crash:
scala> val foo = new Foo
java.lang.NullPointerException
at Bar.<init>(<console>:8)
at Foo.<init>(<console>:6)
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
....
However, if we put chained
in the concrete class:
abstract class Bar[T] {
val f: PartialFunction[T, T]
val default: PartialFunction[T, T] = { case x => x }
}
class Foo extends Bar[Int] {
val f: PartialFunction[Int, Int] = { case 1 => 2 }
val chained = f orElse default
}
It works as expected:
scala> val foo = new Foo
foo: Foo = Foo@16132c4
I have to admit I have absolutely no idea what is happening here. Bug? (This is on Scala 2.8.1.)
"Why is my abstract or overriden val null?" https://github.com/paulp/scala-faq/wiki/Initialization-Order
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With