【发布时间】:2011-12-09 21:32:34
【问题描述】:
我正在使用一个库,它为我提供以下信息(关系可以隐式转换为节点):
class Relation[A,B]
class Node[A,B](r: Relation[A,B])
implicit def relation2node[A,B](r: Relation[A,B]) = new Node(r)
我正在扩展 Relation 以供自己使用:
class XRelation[A] extends Relation[A,Int]
Relations/XRelations 应该被子类化:
class User extends XRelation[Int]
现在,我还定义了自己的 Helper 方法,例如 GET,旨在与任何 Node 以及任何转换为 Node 的东西一起工作:
class Helper[A,B](n: Node[A,B]) { def GET {} }
// note: this is the only way I know of to make the next example work.
implicit def xx2node2helper[A,B,C[_,_]](x: C[A,B])(implicit f: C[A,B] => Node[A,B]) = new Helper(x)
所以这个例子有效:
new Relation[Int,Int]().GET
如果我添加另一个隐式转换:
// don't understand why this doesn't work for the previous example
// but works for the next example
implicit def x2node2helper[A,B,C](x: C)(implicit f: C => Node[A,B]) = new Helper(x)
我还可以进行以下转换:
new XRelation[Int]().GET
但这不起作用:
new User().GET
遗憾的是,这失败了:
error: No implicit view available from Sandbox3.User => Sandbox3.Node[A,B]
谁能理解这一切并解释如何让最后一个示例工作?提前致谢。
更新:我知道你可以只从 Relation 引入隐式转换,但我要求 (1) 弄清楚如何做到这一点,而不必从每个单一类型中引入隐式可能会隐式转换为 Node,并且 (2) 巩固我对隐式的理解。
【问题讨论】: