Extractor: inferred type X arguments do not match unapply method type inference
In the following example, Scala is unable to use the extractor and it drives me crazy:
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]]
This is a test case:
def test[S <: Sys[S]](elem: Element[S]) = elem match {
case Element.Foo(_) => ???
case _ => ???
}
Failed to execute
inferred type arguments [S] do not conform to method unapply type parameter
bounds [S <: Sys[S]]
(both in Scala 2.9.2 and 2.10).
If I remove F-bound it works:
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 _ => ???
}
I think this is one of those "hate Scala days". Is it really that stupid? Basically it is the same as this question , which has no correct answer.
Thank.
+3
source to share
1 answer
When trying to call the parameter test
with the parameter null
and Sys[Any]
, it says that:
type arguments [Sys[Any]] do not conform to trait Element type parameter
bounds [S <: Sys[S]]
Attempted rejection:
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"
-1
source to share