更新:我已经接受了答案,但我仍然很好奇为什么我尝试的方法不起作用(以了解 Scala 隐式行为)。任何其他答案将不胜感激。
(希望这个问题在没有太多 Circumflex 知识的情况下可以回答,但以防万一,这里是文档化的源代码参考。)
我正在尝试在Circumflex ORM库上添加一些便利功能,但在尝试使用 Scala 隐式转换时遇到了一些障碍。下面,为什么隐式转换不触发?我怀疑与子类化和/或递归类型参数存在一些复杂的交互。
import ru.circumflex.orm._
// I subclass Record and Table to add my own convenience methods etc. (not pasted, irrelevant)
abstract class XRecord[PK, R <: XRecord[PK, R]] extends Record[PK, R] { this: R => }
trait XTable[PK, R <: XRecord[PK, R]] extends Table[PK, R] { this: R => }
// Example entity.
class Org extends XRecord[Long,Org] {
val id = "id".BIGINT.NOT_NULL.AUTO_INCREMENT
def PRIMARY_KEY = id
def relation = Org
}
object Org extends Org with XTable[Long,Org]
object Test extends App {
// I want this conversion to work for all Records, not just XRecords.
// Need implicit f to be able to accept XRecord, a subclass of Record.
implicit def toRichRelationNode[PK, R <: Record[PK,R], RR](xs: RR)(implicit f: RR => RelationNode[PK,R]) =
new { def GET(f: RelationNode[PK,R] => Predicate) = 0 }
// This works.
toRichRelationNode(Org) GET (_.id EQ 1)
// This doesn't:
// "No implicit view available from Org.type => ru.circumflex.orm.RelationNode[PK,R]."
Org GET (_.id EQ 1)
}