【发布时间】:2013-11-21 17:58:41
【问题描述】:
假设存在以下类型和方法:
trait X[A <: X[A]]
case class C extends X[C]
def m(x: PartialFunction[X[_], Boolean])
我希望能够创建一个 PartialFunction 以传递给m。
第一次尝试是写
val f: PartialFunction[X[_], Boolean] = {
case c: C => true
}
m(f)
type arguments [_$1] do not conform to trait X's type parameter bounds [A <: X[A]] 失败。所以,看来我们必须约束X 的类型参数。
第二次尝试:
val f: PartialFunction[{type A <: X[A]}, Boolean] = {
case c: C => true
}
m(f)
m 申请失败,因为PartialFunction[AnyRef{type A <: X[this.A]},Boolean] <: PartialFunction[X[_],Boolean] 为假。
在偏函数的定义和m的应用上,有什么方法不涉及实际满足编译器的强制转换?
【问题讨论】:
标签: scala generics types compilation structural-typing