【发布时间】:2011-11-24 12:56:31
【问题描述】:
您好,我阅读了 Debasish 关于隐式函数的有趣帖子。我写了这段代码:
def find[C <: Business](id: String) = {
collection.findOneByID(id).map(x=> implicitly[DBObject => C].apply(x))
}
但它无法使用此编译器消息进行编译:
could not find implicit value for parameter e: (com.mongodb.casbah.commons.Imports.DBObject) => C
我的错是什么?谁能帮帮我?
更新
我的想法是这样的: find 在 trait 中声明 对 DBObject 一无所知,我不想放这个依赖。
trait BusinessRepository {
def find[C <: Business](id: String): Option[C]
}
class MongoBusinessRepository {
val collection = ..
def find[C <: Business](id: String): Option[C] = {
collection.findOneByID(id).map(x=> implicitly[DBObject => C].apply(x))
}
implicit def DBObject2Hotel(x: DBObject): Hotel = {
// ...
// returning Hotel
}
}
case class Hotel(...) extends Business(...)
【问题讨论】: