scala-pickling in POJO in scala 2.11 - Is it really simple?

361 views Asked by At

I'm trying to use scala-pickling because at site github it seems so easy and clean. But, I'm failing in use it in this simple REPL:

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

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

scala> import binary._
import binary._

scala> class Xpto { var a = 0D; var b = 0 }
defined class Xpto

scala> val v = new Xpto { a = 1.23; b = 5 }
v: Xpto = $anon$1@636d2b03

scala> v.pickle
<console>:19: error: type mismatch;
 found   : v.type (with underlying type Xpto)
 required: ?{def pickle: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method PickleOps in package pickling of type [T](picklee: T)pickling.PickleOps[T]
 and method pickleOps in trait Ops of type [T](picklee: T)scala.pickling.PickleOps[T]
 are possible conversion functions from v.type to ?{def pickle: ?}
          v.pickle
            ^
<console>:19: error: value pickle is not a member of Xpto
                  v.pickle
                    ^

What is wrong?

I did access other issues on StackOverflow with this same type of question, for example:

Scala pickling: Simple custom pickler for my own class?

Obs.: I'm using this reference in build.sbt:

"org.scala-lang.modules" %% "scala-pickling" % "0.10.1"
2

There are 2 answers

0
Hélio Moura On

Great! It runs!

I started a new fresh scala console.

I was using this reference to scala.pickling in build.sbt:

"org.scala-lang" %% "scala-pickling" % "0.10.1"

and now I'm using

"org.scala-lang.modules" %% "scala-pickling" % "0.10.1"

I'm also utilising Scala 2.11.6

Now it works perfectly, and really it's so simple.

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

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

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

scala> class Xpto { var a = 0D; var b = 0; }
defined class Xpto

scala> val v = new Xpto { a = 1.23; b = 4; }
v: Xpto = $anon$1@1e7bd4df

scala> v.pickle
res0: pickling.binary.pickleFormat.PickleType = BinaryPickle([0,0,0,52,46,108,105,110,101,55,46,46,114,101,97,100,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,105,119,46,46,97,110,111,110,46,49,63,-13,-82,20,122,-31,71,-82,0,0,0,4])

I don't now if my others libraries references was generating that ambiguous refence. My references in build.sbt are:

libraryDependencies ++= Seq(
  "log4j" % "log4j" % "1.2.17",
  "javax.transaction" % "jta" % "1.1",
  "com.typesafe.akka" %% "akka-actor" % "2.3.10",
  "com.typesafe.akka" %% "akka-testkit" % "2.3.10",
  "org.scalatest" %% "scalatest" % "3.0.0-SNAP4" % "test",
  "org.apache.commons" % "commons-io" % "1.3.2",
  "com.typesafe.akka" %% "akka-slf4j" % "2.3.11",
  "ch.qos.logback" % "logback-classic" % "1.0.9",
  "org.scala-lang.modules" %% "scala-pickling" % "0.10.1"
)

Thanks to Markus.

0
Markus1189 On

Are you sure that those are the only imports you are using when in the REPL? The error above is, as it says:

Note that implicit conversions are not applicable because they are ambiguous: both method PickleOps in package pickling of type [T](picklee: T)pickling.PickleOps[T] and method pickleOps in trait Ops of type [T](picklee: T)scala.pickling.PickleOps[T] are possible conversion functions from v.type to ?{def pickle: ?}

So you have at least two implicit conversions, from scala.pickling.PickleOps[T]() and scala.pickling.Ops.pickleOps. This is strange, because PickleOps is not an implicit class.

For me it works (Scala version 2.11.7 Java 1.7.0_79) in a fresh REPL:

scala> import scala.pickling._
scala> import scala.pickling.Defaults._
scala> import binary._
scala> class Xpto { var a = 0D; var b = 0 }
defined class Xpto
scala> val v = new Xpto { a = 1.23; b = 5 }
v: Xpto = cmd5$$anonfun$1$$anon$1@244da0ed
scala> v.pickle
res6: pickleFormat.PickleType = BinaryPickle([0,0,0,23,99,109,100,53,36,36,97,110,111,110,102,117,110,36,49,36,36,97,110,111,110,36,49,63,-13,-82,20,122,-31,71,-82,0,0,0,5])