scalapattern-matchingexistential-typetype-boundsbounded-quantification

Existential type or type parameter bound failure


I have an F-bounded type Sys:

trait Sys[S <: Sys[S]]

And some trait which take it as type parameter:

trait Foo[S <: Sys[S]]

Suppose I have a method to be invoked with a Foo:

def invoke[S <: Sys[S]](foo: Foo[S]) = ()

Suppose I have a model update type, and a sub-type which carries a Foo:

sealed trait Update
case class Opened[S <: Sys[S]](foo: Foo[S]) extends Update

A helper function to register a model observer:

def observe(pf: PartialFunction[Update, Unit]) = ()

Now the following fails:

observe {
  case Opened(foo) => invoke(foo)
}

with

<console>:16: error: inferred type arguments [Any] do not conform to method invoke's
                     type parameter bounds [S <: Sys[S]]
                case Opened(foo) => invoke(foo)
                                    ^

How can I fix the partial function, if Sys, Foo, invoke, Update, Opened and observe are given. It is allowed to add a value or type member to Foo.


Solution

  • A possible solution is casting:

    observe {
      case Opened(foo) => invoke(foo.asInstanceOf[Foo[~] forSome { type ~ <: Sys[~] }])
    }
    

    Obviously this is horrible and not the preferred solution, so I am waiting for other answers.