scala 2.11中POJO中的scala-pickling -真的很简单吗?



我正在尝试使用scala-pickling,因为在站点github上它看起来很容易和干净。但是,我没有在这个简单的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
                    ^

怎么了?

我确实访问了StackOverflow上的其他问题,例如:

Scala pickling:简单的自定义pickler为我自己的类?

奥林匹克广播服务公司。:我在build.sbt:

中使用此参考
"org.scala-lang.modules" %% "scala-pickling" % "0.10.1"

您确定这些是您在REPL中使用的仅导入吗?上面的错误如下所示:

注意隐式转换不适用,因为它们是二义性的:在类型为[T](picklee: T)的包酸洗中,两个方法都是PickleOps。PickleOps [T]并在类型为[T]的trait Ops (picklee: T)中使用方法pickleOps。从v.type到?{def pickle: ?}的可能转换函数

所以你至少有两个隐式转换,从scala.pickling.PickleOps[T]()scala.pickling.Ops.pickleOps。这很奇怪,因为PickleOps不是隐式类。

对我来说,它的工作(Scala version 2.11.7 Java 1.7.0_79)在一个新的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])

太棒了!它运行!

我启动了一个新的scala控制台。

我正在使用这个对scala的引用。build.sbt:

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

现在我用

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

我也使用Scala 2.11.6

现在它工作得很好,真的很简单。

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

我不知道我的其他库引用是否生成了那个模糊的引用。我在构建中的引用。sbt :

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

感谢Markus

相关内容

  • 没有找到相关文章