1

我正在尝试使用 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酸洗:我自己的班级的简单自定义酸洗器?

Obs.:我在build.sbt中使用了这个参考:

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

2 回答 2

0

伟大的!它运行!

我开始了一个新的全新 scala 控制台。

我在 build.sbt 中使用了对 scala.pickling 的引用:

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

如果我的其他库引用产生了模棱两可的引用,我现在不会。我在build.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"
)

感谢马库斯。

于 2015-08-23T14:04:03.940 回答
0

您确定这些是您在 REPL 中使用的唯一导入吗?上面的错误是,正如它所说:

请注意,隐式转换不适用,因为它们不明确:类型为 [T](picklee: T)pickling.PickleOps[T] 的包酸洗中的 PickleOps 方法和类型为 [T](picklee: T) 的特征 Ops 中的方法 pickleOps scala.pickling.PickleOps[T] 是从 v.type 到 ?{def pickle: ?} 的可能转换函数

所以你至少有两个隐式转换, fromscala.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])
于 2015-08-23T07:32:03.033 回答