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

scala.NotImplementedError when using custom function to unpickle case class to its parent trait type #38

Closed
mraulim opened this issue Sep 26, 2013 · 1 comment
Labels

Comments

@mraulim
Copy link

mraulim commented Sep 26, 2013

Hello, here is the problem:

scala> import scala.pickling._
import scala.pickling._

scala> import json._
import json._

scala> trait Fruit
defined trait Fruit

scala> case class Apple() extends Fruit
defined class Apple

// This works just fine: 
scala> Apple().pickle.unpickle[Fruit]
res0: Fruit = Apple()

but when I define function to unpickle any instance of type T or any instance derived from trait T, it fails

scala> def unpickle[T : Unpickler : FastTypeTag](p : JSONPickle) = p.unpickle[T]
unpickle: [T](p: scala.pickling.json.JSONPickle)(implicit evidence$1: scala.pickling.Unpickler[T], implicit evidence$2: scala.pickling.FastTypeTag[T])T

scala> unpickle[Fruit](Apple().pickle)

problem

scala> unpickle[Fruit](Apple().pickle)
scala.NotImplementedError: an implementation is missing
    at scala.Predef$.$qmark$qmark$qmark(Predef.scala:252)
    at scala.pickling.PicklerUnpicklerNotFound.unpickle(Custom.scala:20)
    at .unpickle(<console>:13)
    at .<init>(<console>:18)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
    at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
    at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:889)
    at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
    at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77)
    at sbt.Console.sbt$Console$$console0$1(Console.scala:23)
    at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
    at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33)
    at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)

Function works fine with concrete type:
scala> unpickle[Apple](Apple().pickle)
res1: Apple = Apple()

Thanks a lot,
Milan

@eed3si9n
Copy link
Member

eed3si9n commented Feb 8, 2015

I can't reproduce this using Pickling 0.10.0.

Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.pickling._, Defaults._
import scala.pickling._
import Defaults._

scala> import json._
import json._

scala> :paste
// Entering paste mode (ctrl-D to finish)

trait Fruit
case class Apple() extends Fruit

// Exiting paste mode, now interpreting.

defined trait Fruit
defined class Apple

scala> Apple().pickle.unpickle[Fruit]
res0: Fruit = Apple()

scala> def unpickle[T : Unpickler : FastTypeTag](p : JSONPickle) = p.unpickle[T]
unpickle: [T](p: scala.pickling.json.JSONPickle)(implicit evidence$1: scala.pickling.Unpickler[T], implicit evidence$2: scala.pickling.FastTypeTag[T])T

scala> unpickle[Fruit](Apple().pickle)
res1: Fruit = Apple()

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

No branches or pull requests

2 participants