I am having trouble with path dependent types and pattern matching:
trait View[A]
trait Foo {
type Bar
def defaultBar: Bar
}
trait Baz extends Foo {
def view(init: Bar): View[Bar]
}
trait Test {
val foo: Foo
def bar: foo.Bar = foo.defaultBar
def test(): Option[View[foo.Bar]] =
foo match {
case b: Baz => Some(b.view(bar))
case _ => None
}
}
This fails because scalac doesn't identify foo
with b
. As a result, it only works with two casts:
case b: Baz => Some(b.view(bar.asInstanceOf[b.Bar]).asInstanceOf[View[foo.Bar]])
Surely there must be a clean way to avoid the casts?