Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

SPickler does not support traits #3

Closed
mathieuleclaire opened this issue Jun 19, 2013 · 5 comments
Closed

SPickler does not support traits #3

mathieuleclaire opened this issue Jun 19, 2013 · 5 comments

Comments

@mathieuleclaire
Copy link

I try to defined a dummy SPickler this way:

class CustomPickler[T](implicit val format: PickleFormat) extends SPickler[T]{
  def pickle(t: T, builder: PBuilder): Unit = {
    println("In my pickle")
    builder.beginEntry(t)
    builder.endEntry
  }
}

then I defined an implicit this way:

  implicit def cPickler = new CustomPickler[X]
  println((new Y(0)).pickle)

where Y extends the trait X:

trait X {
  def id: Int
}

class Y(val id: Int) extends X

but it does not apply the the pickle method I defined in the CustomPickler class, whereas it does if replacing X by Y in the definition of the implicit. Conclusion: SPickler does not support the trait (if I correctly coded the things), which is a big limitation.

@markehammons
Copy link

Matthieu, I think this does what you want:

implicit def cPickler[T <: X] = new CustomPickler[T]

@heathermiller
Copy link
Member

We don't have much for documentation (yet) of scala-pickling (besides an academic paper which covers some of these things, but is definitely not ideal for developers). Apologies– comprehensive documentation is on the way.

An important detail that you might not know is that SPickler[T] is meant to pickle only objects of type T, but not subtypes of T.

That means that when you define:

    implicit def cPickler = new CustomPickler[X]

A CustomPickler[X] is introduced in the implicit scope, but CustomPicklers for any of X's subtypes are not introduced into implicit scope. So, @markehammons is correct– the easiest way, when supplying your own custom picklers, and dealing with subtypes, is to be explicit that your custom pickler is also suitable for subtypes of X:

    implicit def cPickler[T <: X] = new CustomPickler[T]

@heathermiller
Copy link
Member

Could you let us know if this clarifies things/solves your problem?

@mathieuleclaire
Copy link
Author

It clarifies this point yes, thanks. I stay tuned for more documentation then !

@heathermiller
Copy link
Member

Awesome- thanks, closing this then. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants