I'm trying to compile the code below which looks correct, although the compiler refuses it. I'm probably doing something wrong or I need to hint the compiler somehow.
sealed trait Foo[A]
case class Bar[A, B <: A]() extends Foo[A]
class Visitor[A](foo: Foo[A]) {
def onBar[B <: A](bar: Bar[A, B]) = ???
def run = foo match
case b: Bar[A, b] => onBar[b](b)
}
The error is
Type argument b does not conform to upper bound A
This issue is currently being discussed at scala/scala3#20040, and the question's code's validity will be confirmed upon resolution of that report. For the present, there exists two workarounds:
FooandBar, you can write afoldmethod to circumvent the need for a pattern match:matchlayer that forces the compiler to rememberBis related toA: