the following code errors with a ‘type mismatch’ error, saying that FooProcessor should be Processor[M].
sealed trait Model
case class Foo extends Model
trait Processor[M <: Model]
class FooProcessor extends Processor[Foo]
class DelegatingProcessor[M <: Model] extends Processor[M] {
val delegates = Map[String, Processor[M]]("foo" -> new FooProcessor())
}
How to you convince the compiler that FooProcessor is an extension of Processor[Model]?
The short answer is that your
FooProcessoris an extension ofProcessor[Foo], and is specific toFoo. InDelegatingProcessor, you need aProcessorthat is able to handle not onlyFoo, but any validModel.FooProcessorsimply doesn’t fit the bill here. And — don’t try to convince the compiler otherwise, because the compiler is here exactly to prevent this kind of mistakes 🙂