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

Compilation failure when using type parameters #31

Closed
emchristiansen opened this issue Sep 11, 2013 · 7 comments
Closed

Compilation failure when using type parameters #31

emchristiansen opened this issue Sep 11, 2013 · 7 comments

Comments

@emchristiansen
Copy link
Contributor

Please see this StackOverflow post:

http://stackoverflow.com/questions/18725699/scala-pickling-and-type-parameters

This is either a bug or user error.
If it is user error, perhaps the documentation should be updated.

@heathermiller
Copy link
Member

Firstly, thanks a lot for reporting and cross-linking with SO. We really appreciate it. We're looking into this as we speak.

Note: this ticket is related to (seems like a duplicate?) issue #4

@heathermiller
Copy link
Member

It seems that the pickling macro is too restrictive in this case. It fails to actually use the implicit SPickler that's in scope, which it should use. Basically, it just gives up because the type is abstract, and there should be no reason for this.

@heathermiller
Copy link
Member

So this is a bug. To resolve this, I think we just need to update the dispatch logic that's generated by the macro. Looking into it now...

@heathermiller
Copy link
Member

Fixed in commit 17aac11

With this fix + the use of a "FastTypeTag", all you should have to do is:

def bar[T: SPickler: FastTypeTag](t: T) = t.pickle

So now you can use bar like so:

val p = PersonY("Philipp", 32)
assert(bar(p).value == p.pickle.value)
assert(bar(42).unpickle[Int] == 42)

Added these as tests in https://github.com/scala/pickling/blob/2.10.x/core/src/test/scala/pickling/generic-spickler.scala

@heathermiller
Copy link
Member

Note: will provide more extensive documentation on this as well :)

@emchristiansen
Copy link
Contributor Author

That worked for pickling, but unpickling seems still broken.
For example, attempting to compile:

def unbar[A: SPickler: FastTypeTag](bytes: Array[Byte]) = 
    BinaryPickle(bytes).unpickle[A]

Results in the error:

[error] /Users/eric/Dropbox/t/2013_q3/pickledb/src/main/scala/PickleDB.scala:21: unpickle needs an explicitly provided type argument
[error]     BinaryPickle(bytes).unpickle[A]

@heathermiller
Copy link
Member

Yep, thanks for catching it. A small tweak on our end + a small tweak to what you're trying will do the trick.

Instead of an SPickler, make sure you use an Unpickler on the unpickling side:

def unbar[T: Unpickler: FastTypeTag](s: String) = JSONPickle(s).unpickle[T]

(This is also now in the same test file, https://github.com/scala/pickling/blob/2.10.x/core/src/test/scala/pickling/generic-spickler.scala. It's JSON because what's in the test is JSON, but in the SO article linked to above, you'll see the same example with binary)

phaller pushed a commit to phaller/pickling that referenced this issue Jul 27, 2015
phaller pushed a commit to phaller/pickling that referenced this issue Jul 27, 2015
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

2 participants