I've got error for generating pickler/unpickler for a type with type-parameter

643 Views Asked by At

I'm trying to use scala-pickling for my project; but I've got a problem with it. let assume that I've got this code:

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]]

When I'm try to hideType.pickle , I get a compile-time error:

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

What's the problem for generating a pickler/unpickler in this case?

more info:

scala 2.11.6

scala-pickling 0.10.0

EDIT 1:

result of compile with "-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

There are 1 best solutions below

0
On

I am not sure what is the intention but you got a generic variable.

Try

hideType[State].pickle 

or

hideType[Married].pickle