There seems to be a limitation in which you cannot use PartialFunction literals in class constructors:
scala> case class X(a: PartialFunction[Any, Any]) { def this() = this({case x => x}) } <console>:7: error: Implementation restriction: <$anon: Any => Any> requires premature access to class X. case class X(a: PartialFunction[Any, Any]) { def this() = this({ case x => x}) }
My first question is why a partial function private literal needs access to "this". My second question / observation is that in Scala REPL, running the same code again causes a REPL error:
scala> case class X(a: PartialFunction[Any, Any]) { def this() = this({ case x => x}) } java.lang.NullPointerException at scala.tools.nsc.Global$Run.compileLate(Global.scala:1595) at scala.tools.nsc.GlobalSymbolLoaders.compileLate(GlobalSymbolLoaders.scala:29) at scala.tools.nsc.symtab.SymbolLoaders$SourcefileLoader.doComplete(SymbolLoaders.scala:369) ...
And finally, is there a good way to solve this problem?
source share