Why Scala can serialize Function but not PartialFunction?

蹲街弑〆低调 提交于 2019-12-02 00:06:36

Because you are explicitly creating AbstractPartialFunction without extending Serializable. If you do the same with new AbstractFunction1[String, Int] { ... }, it also won't be serializable. On the other hand, when you use the anonymous function syntax, the compiler generates a class which does extend Serializable. This includes anonymous partial function syntax:

scala> val x: PartialFunction[Int, Boolean] = { case 0 => true }
x: PartialFunction[Int,Boolean] = <function1>

scala> x.isInstanceOf[Serializable]
res0: Boolean = true
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!