i have 2 scala classes (paraphrased):
abstract class genericparser[t] { val linefilter : string => boolean parsedata() def parsedata() : t { for( line <- .... if linefilter(line) ) // things } } class salesparser extends genericparser[salesrow] { val linefilter = line => !line.startswith("//") // .... }
the problem linefilter
null
in parsedata
, presumably because parsedata
called while primary genericparser
constructor still running, subclass hasn't initialized members.
i can work around making linefilter def
instead of val
, expected behavior? doesn't seem right problem should become apparent after getting npe @ runtime.
it indeed expected behavior, , same problem in question:
scala 2.8: how initialize child class
you can copy-paste answer form question. solutions include:
def
orlazy val
instead ofval
- early initialization of
linefilter
redesign of classes avoid “virtual method call superclass's constructor accesses uninitialized subclass values” problem. instance, why want store filter function in val or return in def, while implemented method?
abstract class genericparser[t] { def linefilter(line: string): boolean parsedata() def parsedata() : t { for( line <- .... if linefilter(line) ) // things } } class salesparser extends genericparser[salesrow] { def linefilter(line: string) = !line.startswith("//") }
Comments
Post a Comment