पर विचार करें निम्नलिखित (सरलीकृत) उदाहरण में ओवरराइड वैल तक पहुँचने:NullPointerException जब सार निर्माता
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 }
}
और यह दुर्घटना देखने के लिए:
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>)
....
हालांकि, अगर हम ठोस कक्षा में chained
डाल:
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
}
यह अपेक्षा के अनुसार काम करता है:
scala> val foo = new Foo
foo: Foo = [email protected]
मुझे यह मानना है कि मुझे बिल्कुल पता नहीं है कि यहां क्या हो रहा है। बग? (यह स्कैला 2.8.1 पर है।)
इस प्रश्न को समाधान/समाधान के लिए देखें: http://stackoverflow.com/questions/4712468/in-scala-what-is-an-early-initializer – Raphael