我在为带有类型参数的类型生成拾取器/取消拾取器时出错



我正在尝试使用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 -implicit "编译的结果:

[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

我不确定是什么意图,但你得到了一个通用变量。

hideType[State].pickle 

hideType[Married].pickle

相关内容

  • 没有找到相关文章