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

让人想犯罪 __ 提交于 2019-12-24 11:27:51

问题


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"

回答1:


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])



回答2:


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.



来源:https://stackoverflow.com/questions/32156930/scala-pickling-in-pojo-in-scala-2-11-is-it-really-simple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!