I'm using Scala Pickling, an automatic serialization framework for Scala.
According to the author's slides, any type T
can be pickled as long as there is an implicit Pickler[T]
in scope.
Here, I'm assuming she means scala.tools.nsc.io.Pickler
.
However, the following does not compile:
import scala.pickling._
import scala.pickling.binary._
import scala.tools.nsc.io.Pickler
object Foo {
def bar[T: Pickler](t: T) = t.pickle
}
The error is:
[error] exception during macro expansion:
[error] scala.ScalaReflectionException: type T is not a class
[error] at scala.reflect.api.Symbols$SymbolApi$class.asClass(Symbols.scala:323)
[error] at scala.reflect.internal.Symbols$SymbolContextApiImpl.asClass(Symbols.scala:73)
[error] at scala.pickling.PickleMacros$class.pickleInto(Macros.scala:381)
[error] at scala.pickling.Compat$$anon$17.pickleInto(Compat.scala:33)
[error] at scala.pickling.Compat$.PickleMacros_pickleInto(Compat.scala:34)
I'm using Scala 2.10.2 with scala-pickling 0.8-SNAPSHOT.
Is this a bug or user error?
EDIT 1: The same error arises with both scala.pickling.SPickler
and scala.pickling.DPickler
.
EDIT 2: It looks like this is a bug: https://github.com/scala/pickling/issues/31
Looking at the project, it seems you need either an
scala.pickling.SPickler
or ascala.pickling.DPickler
(static and dynamic, respectively) in order to pickle a particular type.The pickle methods are macros. I suspect that if you pickle with an
SPickler
, the macro will require the compile time type of your class to be known.Thus, you may need to do something similar to:
Alternatively, a
DPickler
may do the trick. I suspect that you'll still have to write some custom pickling logic for your specific types.Yep, as Andy pointed out:
Those both already come in the
scala.pickling
package, so it's enough to just use them in your generic method signature.You're absolutely correct that you can add an
SPickler
context-bound to your generic method. The only additional thing which you need (admittedly it's a bit ugly, and we're thinking about removing it) is to add aFastTypeTag
context bound as well. (This is necessary for the pickling framework to know what type it's trying to pickle, as it handles primitives differently, for example.)This is what you'd need to do to provide generic pickling/unpickling methods:
Note that for the
unbar
method, you need to provide anUnpickler
context-bound rather than aSPickler
context-bound.Testing this in the REPL, you get: