Scala:如何让上下文绑定的 List[T] 转换在这里工作?
这是我的第一个问题,所以希望我提供足够的细节。请随时要求澄清。
考虑到以下因素,这是有效的:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的listBsonReader中,没有理由输入U。你的光标在DBObject上迭代,map需要一个
DbObject =>; X
函数。我猜你有类似In map 的内容,
_
类型为DBObject
,通常是从BsonReader[T, DBObject]
中查看。您在隐式作用域中不提供任何内容,仅提供BsonReader[T,U]
。只需删除U
并将隐式参数设置为BsonReader[T,DBObject]
即可。编译器不会尝试提前计算
T
。它试图确保无论T
和U
可能位于调用站点(在本例中,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 likeIn map, with
_
typedDBObject
, it is, quite normally looking fromBsonReader[T, DBObject]
. You provide none in implicit scope, only aBsonReader[T,U]
. Just removeU
and have your implicit parameter beBsonReader[T,DBObject]
.The compiler is not trying to evaluate
T
in advance. It is trying to ensure that whateverT
andU
may be at call site, (in this case,U
is the problem), it will have the implicitBSonReader[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 oneBsonReader[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 callingfromBson
), you would have a similar error.