2

我正在尝试为我的项目使用 scala-pickling;但我有一个问题。假设我有这个代码:

import scala.pickling._
import scala.pickling.Defaults._
import scala.pickling.json._

sealed trait State
case class Married(name:String) extends State
case object Single extends State

trait Person[T<:State] {
   def name:String
   def surname:String
   def age:Int
   def state:T
}

case class Male[T<:State](
   val name:String,
   val surname:String,
   val age:Int,
   val state:T) extends Person[T]

case class Female[T<:State](
   val name:String,
   val surname:String,
   val age:Int,
   val state:T) extends Person[T]


def hideType[T<:State]:Person[T] = Male("Hussein", "?", 145, Single).asInstanceOf[Person[T]]

当我尝试时hideType.pickle,我得到一个编译时错误:

error: Cannot generate a pickler for Person[T]. Recompile with -Xlog-implicits for details

在这种情况下生成pickler/unpickler有什么问题?

更多信息:

斯卡拉 2.11.6

scala-pickling 0.10.0

编辑1:

使用“-Xlog-implicits”编译的结果:

[info] Loading global plugins from /home/someone/i/etc/sbt/0.13/plugins
[info] Loading project definition from /home/someone/tmp/pickling/project
[info] Set current project to pickling (in build file:/home/someone/tmp/pickling/)
[info] Compiling 1 Scala source to /home/someone/tmp/pickling/target/scala-2.11/classes...
[info] /home/someone/tmp/pickling/src/main/scala/com/example/Hello.scala:35: genPickler is not a valid implicit value for scala.pickling.Pickler[com.example.Person[T]] because:
[info] hasMatchingSymbol reported error: stepping aside: repeating itself
[info]    hideType.pickle
[info]             ^
[info] /home/someone/tmp/pickling/src/main/scala/com/example/Hello.scala:35: genPickler is not a valid implicit value for scala.pickling.Pickler[com.example.Person[T]] because:
[info] hasMatchingSymbol reported error: polymorphic expression cannot be instantiated to expected type;
[info]  found   : [T]scala.pickling.Pickler[T]
[info]  required: scala.pickling.Pickler[com.example.Person[?]]
[info]    hideType.pickle
[info]             ^
[error] /home/someone/tmp/pickling/src/main/scala/com/example/Hello.scala:35: Cannot generate a pickler for com.example.Person[T]. Recompile with -Xlog-implicits for details
[error]    hideType.pickle
[error]             ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 2 s, completed May 26, 2015 4:31:45 AM
4

1 回答 1

0

我不确定目的是什么,但你有一个通用变量。

尝试

hideType[State].pickle 

或者

hideType[Married].pickle
于 2015-05-26T17:34:10.683 回答