假设存在以下类型和方法:
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
?