为类型参数为

时间:2015-05-26 09:24:40

标签: scala scala-pickling

我正在尝试使用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有什么问题?

更多信息:

scala 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

1 个答案:

答案 0 :(得分:0)

我不确定是什么意思但你有一个通用变量。

尝试

hideType[State].pickle 

hideType[Married].pickle