scala / pickling

Fast, customizable, boilerplate-free pickling support for Scala
lampwww.epfl.ch/~hmiller/pickling
BSD 3-Clause "New" or "Revised" License
831 stars 79 forks source link

FastTypeTag doesn't work for types with type parameters #417

Open jvican opened 8 years ago

jvican commented 8 years ago

I wonder why this happens:

case class Map[T, S]( ... )
val tag = implicitly[FastTypeTag[Map[List[Int], List[String]]]]
println(tag) // shows Map[t,s] where t,s are type variables

When using AnyUnpickler one wants to unpickle anything inferred from its type. In this case, reflection fails to create an unpickler for that type because t and s are unknown. Why aren't t and s concrete when they could be List[Int] and List[String]? Is this a bug? Otherwise, why doesn't FastTypeTag constructs the tag from the full type?

/cc @jsuereth since he's been refactoring FastTypeTag lately.

jsuereth commented 8 years ago

@jvican Looks like that's a bug. There's a bit of funny-behavior regarding type parameters (usually my fault) in the macros.

jvican commented 8 years ago

Good, tomorrow I'll work on a fix for this.

jvican commented 8 years ago

The problem here is that we create FastTypeTags for type variables. I'm not sure if we should. Let me explain it with a bit of code:

trait Foo
case class Bar[T](v: T)
case class Zaz(i: Int)

Now, let's create a concrete pickler for Foo:

def pickle(f: Foo, b: PBuilder): Unit = {
  f match {
    case b: Bar[t] =>
      // t is a type variable and BarPickle gets an implicit `FastTypeTag`
      // if we do not put a concrete tag here, one will be created for `t`
      barPicklerUnpickler.pickle(b)
    case z: Zaz => ???
  }
}

I'm not very sure if this behavior is intended by scala pickling. But since a FastTypeTag is a witness of a type, and we expect this type to be concrete, wouldn't be sensible to still consider this as a bug?

jsuereth commented 8 years ago

yes, I agree it looks like a bug. We shouldn't be able to grab a static type tag without some witness of t's fast type tag