Scala 提取器:推断的类型参数X不符合方法unapply';s型参数界
在以下示例中,Scala无法使用提取器,这让我抓狂:Scala 提取器:推断的类型参数X不符合方法unapply';s型参数界,scala,pattern-matching,type-bounds,Scala,Pattern Matching,Type Bounds,在以下示例中,Scala无法使用提取器,这让我抓狂: trait Sys[S <: Sys[S]] object Element { object Foo { def unapply[S <: Sys[S]](foo: Foo[S]): Option[Any] = ??? } trait Foo[S <: Sys[S]] extends Element[S] } trait Element[S <: Sys[S]] trait Sys[S当尝试使用
trait Sys[S <: Sys[S]]
object Element {
object Foo {
def unapply[S <: Sys[S]](foo: Foo[S]): Option[Any] = ???
}
trait Foo[S <: Sys[S]] extends Element[S]
}
trait Element[S <: Sys[S]]
trait Sys[S当尝试使用null
参数和Sys[Any]
类型参数调用test
时,它确实表示:
type arguments [Sys[Any]] do not conform to trait Element's type parameter
bounds [S <: Sys[S]]
type参数[Sys[Any]]与trait元素的类型参数不一致
界限
inferred type arguments [S] do not conform to method unapply's type parameter
bounds [S <: Sys[S]]
trait Sys
object Element {
object Foo {
def unapply[S <: Sys](foo: Foo[S]): Option[Any] = ???
}
trait Foo[S <: Sys] extends Element[S]
}
trait Element[S <: Sys]
def test[S <: Sys](elem: Element[S]) = elem match {
case Element.Foo(_) => ???
case _ => ???
}
type arguments [Sys[Any]] do not conform to trait Element's type parameter
bounds [S <: Sys[S]]
trait Sys[-S]
object Element {
object Foo {
def unapply[S <: Sys[S]](foo: Foo[S]): Option[Any] = ???
}
trait Foo[S <: Sys[S]] extends Element[S]
}
trait Element[S <: Sys[S]]
def test[S <: Sys[S]](elem: Element[S]) = elem match {
case f: Element.Foo[S] => "ok"
case _ => "smth else"
}
// test
test(new Element.Foo[Sys[Any]](){}) // "smth else"
test(new Element[Sys[Any]](){}) // "ok"