似乎有一个限制,即您不能在类构造函数中使用PartialFunction
字面量:
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}) }
我的第一个问题是,为什么部分类函数需要访问"this"。第二个问题/观察结果是,在Scala REPL中,再次运行相同的代码会导致REPL崩溃:
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)
...
最后,有没有一个好的解决方法来解决这个问题?