gpt4 book ai didi

mongodb - ADT 的编解码器不编译

转载 作者:行者123 更新时间:2023-12-04 14:24:47 28 4
gpt4 key购买 nike

我正在使用 scala 驱动程序与 mongodb 进行 IO 操作。我的Scala版本是2.11.11 mongo db 驱动程序是 2.2.0 .

我以关于 ADT 的文档为例:

sealed class Tree
case class Branch(b1: Tree, b2: Tree, value: Int) extends Tree
case class Leaf(value: Int) extends Tree

val codecRegistry = fromRegistries( fromProviders(classOf[Tree]), DEFAULT_CODEC_REGISTRY )

这段代码没有编译。
No known subclasses of the sealed class
[error] val codecRegistry = fromRegistries( fromProviders(classOf[Tree]), DEFAULT_CODEC_REGISTRY )
[error] ^
[error] knownDirectSubclasses of Tree observed before subclass Branch registered
[error] knownDirectSubclasses of Tree observed before subclass Leaf registered

我错过了什么 ?

更新

下面是我正在做的事情的完整示例。
build.sbt
name := "mongodb-driver-test"

version := "1.0"

scalaVersion := "2.11.11"

libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "2.2.0"

文件 Models.scala
import org.mongodb.scala.bson.codecs.{DEFAULT_CODEC_REGISTRY, Macros}
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}

/**
* Created by alifirat on 02/01/18.
*/


object Models {

sealed class Tree
case class Branch(b1: Tree, b2: Tree, value: Int) extends Tree
case class Leaf(value: Int) extends Tree

val treeCodec = Macros.createCodecProvider[Tree]()

val treeCodecRegistry = fromRegistries( fromProviders(treeCodec), DEFAULT_CODEC_REGISTRY )

}

然后,只需执行以下操作:
sbt compile
你会得到 :
[error]   val treeCodec = Macros.createCodecProvider[Tree]()
[error] ^
[error] knownDirectSubclasses of Tree observed before subclass Branch registered
[error] knownDirectSubclasses of Tree observed before subclass Leaf registered
[error] three errors found
[error] (compile:compileIncremental) Compilation failed

如果我将 Scala 版本更改为 2.12.0 , 我在编译时没有任何错误...

最佳答案

我正在使用驱动程序版本 2.6.0 和 Scala 版本 2.12.8,但仍然遇到同样的问题。

我的解决方法是删除关键字 sealed前面那个密封类,编译,放回去,再编译。但是非常麻烦。

关于mongodb - ADT 的编解码器不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48059378/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com