Scala:如何让上下文绑定的 List[T] 转换在这里工作?

发布于 2024-12-03 05:34:40 字数 1590 浏览 15 评论 0原文

这是我的第一个问题,所以希望我提供足够的细节。请随时要求澄清。

考虑到以下因素,这是有效的:

implicit def optionBsonReader[T, U](implicit ev: BsonReader[T, U]) = new BsonReader[Option[T], Option[U]] {
  def read(obj: Option[U]): Option[T] = {
    obj match {
      case Some(x) => Some(x.fromBson[T])
      case None => None
    }
  }
}

这段代码将 Option 包装的 BSON 片段转换为另一个 Option[T]。我认为这同样适用于列表,但以下内容无法编译:

implicit def listBsonReader[T, DBObject](implicit ev: BsonReader[T, DBObject]) = new BsonReader[List[T], MongoCursor] {
  def read(cur: MongoCursor): List[T] = {
    cur.map(_.fromBson[T]).toList
  }
}

我正在使用以下代码来实现一般机制:

package object bson {

  def bsonReader[A, B](implicit reader: BsonReader[A, B]) = reader
  def bsonWriter[A, B](implicit writer: BsonWriter[A, B]) = writer

  implicit def addWriter[A](any: A): WithWriter[A] = new WithWriter(any)
  implicit def addReader[A](any: A): WithReader[A] = new WithReader(any)
}

package bson {
  private[bson] class WithWriter[A](any: A) {
    def toBson[B](implicit writer: BsonWriter[A, B]): B = writer.write(any)
  }
  private [bson] class WithReader[B](any: B) {
    def fromBson[A](implicit reader: BsonReader[A, B]): A = reader.read(any)
  }
}

编译器错误: 无法找到参数读取器的隐式值:project.marshalling.bson.BsonReader[T,com.mongodb.casbah.Imports.DBObject] cur.map(_.fromBson[T]).toList

这让我觉得很奇怪,因为编译器似乎试图在调用 fromBson 来提供类型之前评估 T 。这让我觉得特别奇怪,因为选项读者似乎没有这样的抱怨。我最近才开始认真地用 Scala 编写代码,所以我确信我在这里遗漏了一些东西。

如果您需要更多信息,请告诉我并希望您能提供帮助。

最好的,

德克

This is my first question here so hope I provide enough detail. Feel free to ask for clarification.

Taking the following into consideration, which works:

implicit def optionBsonReader[T, U](implicit ev: BsonReader[T, U]) = new BsonReader[Option[T], Option[U]] {
  def read(obj: Option[U]): Option[T] = {
    obj match {
      case Some(x) => Some(x.fromBson[T])
      case None => None
    }
  }
}

This piece of code converts Option wrapped pieces of BSON to another Option[T]. I thought that the same would work for lists but the following doesn't compile:

implicit def listBsonReader[T, DBObject](implicit ev: BsonReader[T, DBObject]) = new BsonReader[List[T], MongoCursor] {
  def read(cur: MongoCursor): List[T] = {
    cur.map(_.fromBson[T]).toList
  }
}

I am using the following code for the general mechanics:

package object bson {

  def bsonReader[A, B](implicit reader: BsonReader[A, B]) = reader
  def bsonWriter[A, B](implicit writer: BsonWriter[A, B]) = writer

  implicit def addWriter[A](any: A): WithWriter[A] = new WithWriter(any)
  implicit def addReader[A](any: A): WithReader[A] = new WithReader(any)
}

package bson {
  private[bson] class WithWriter[A](any: A) {
    def toBson[B](implicit writer: BsonWriter[A, B]): B = writer.write(any)
  }
  private [bson] class WithReader[B](any: B) {
    def fromBson[A](implicit reader: BsonReader[A, B]): A = reader.read(any)
  }
}

Compiler error:
could not find implicit value for parameter reader: project.marshalling.bson.BsonReader[T,com.mongodb.casbah.Imports.DBObject] cur.map(_.fromBson[T]).toList

This strikes me as odd since it seems like the compiler is trying to evaluate T before fromBson has been called to supply a type. This strikes me as especially odd since the option reader seems to have no such complaints. I've only recently started to code in Scala in earnest so I am sure I am missing something here.

Please let me know if you need more info and hope you can help.

Best,

Dirk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-12-10 05:34:40

在你的listBsonReader中,没有理由输入U。你的光标在DBObject上迭代,map需要一个DbObject =>; X 函数。我猜你有类似

implicit def withFromBson[U](x: U) = new {
   def fromBson[T](implicit ev : BsonReader[T,U]) : T = ...
}

In map 的内容,_ 类型为 DBObject,通常是从 BsonReader[T, DBObject] 中查看。您在隐式作用域中不提供任何内容,仅提供 BsonReader[T,U]。只需删除 U 并将隐式参数设置为 BsonReader[T,DBObject] 即可。

编译器不会尝试提前计算 T。它试图确保无论 TU 可能位于调用站点(在本例中,U 就是问题),它都会具有隐式作用域中所需的隐式 BSonReader[T, DBObject] 。我想一般环境下是没有的。您通过隐式参数承诺,您将在调用站点提供一个 BsonReader[T,U]。这不是它所需要的。如果参数不是隐式的(调用 fromBson 时必须编写 ev at),则会出现类似的错误。

In your listBsonReader, there is no reason for type U. Your cursor iterate on DBObject, map expects a DbObject => X function. I guess you have something like

implicit def withFromBson[U](x: U) = new {
   def fromBson[T](implicit ev : BsonReader[T,U]) : T = ...
}

In map, with _ typed DBObject, it is, quite normally looking from BsonReader[T, DBObject]. You provide none in implicit scope, only a BsonReader[T,U]. Just remove U and have your implicit parameter be BsonReader[T,DBObject].

The compiler is not trying to evaluate T in advance. It is trying to ensure that whatever T and U may be at call site, (in this case, U is the problem), it will have the implicit BSonReader[T, DBObject] it needs in implicit scope. I guess there is none in the general environment. You promise, with your implicit parameter, that you will give one BsonReader[T,U] at call site. That is not what it needs. Where the parameter not implicit (you would have to write the ev at when calling fromBson), you would have a similar error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文