以下是我遇到的问题的简化版本:
case class HEdge[N,E](n:N, e:E)
case class ANode[N,E](head:N ,out:List[HEdge[N,E]])
object ANode{
def apply[N,E](head:N ,out:HEdge[N,E]*):ANode[N,E] = ANode(head,out.toList)
}
当我想将它与简单类型一起使用时,它按预期工作:
val x = ANode[String,String]("aaa", HEdge("bb","cc"))
我还用另一种更复杂的类型测试它:
case class Foo[N](e:N => Boolean)
case class Bar[E](e:E => Boolean)
当我在Foo
和Bar
内完全指定函数的类型参数时,它会起作用:
val y1 = ANode[Foo[String], Bar[String]](Foo[String](x => x == ""), HEdge(Foo[String](x => x == ""), Bar[String](z => z == "")))
但是当我把它留给编译器来查找类型时它不起作用:
val y = ANode[Foo[String], Bar[String]](Foo(x => x == ""), HEdge(Foo(x => x == ""), Bar(z => z == "")))
如何强制编译器正常工作?
答案 0 :(得分:4)
case class ANode[N,E](head:N ,out:List[HEdge[N,E]])
定义了它自己的apply
,你重载它会引起歧义。这将编译:
case class HEdge[N,E](n:N, e:E)
class ANode[N,E](head:N ,out:List[HEdge[N,E]])
object ANode{
def apply[N,E](head:N ,out:HEdge[N,E]*):ANode[N,E] = new ANode(head,out.toList)
}
case class Foo[N](e:N => Boolean)
case class Bar[E](e:E => Boolean)
val y1 = ANode[Foo[String], Bar[String]](Foo(x => x == ""), HEdge(Foo(x => x == ""), Bar(z => z == "")))