Weird match type reduction failure

Given the match type below

type MatchType[T] = T match
  case Bar[?] => Nothing
  case _ => T

I get the following error when using it somewhere else (unfortunately, the full code is hard to minimize):

trying to reduce MatchType[Bar[? <: Foo]] failed since selector Bar[? <: Foo] does not match case Bar[_] => Nothing and cannot be shown to be disjoint from it either.

How come Bar[? <: Foo] doesn’t match Bar[?]? And how can I write a type pattern that ensures a match against Bar[? <: Foo]?

What’s Foo and what’s Bar? Can you share a Scastie maybe?

Here is a minimized example.

1 Like

I don’t fully understand (hopefully the compiler people can explain better) but it seems that the type parameter of Bar[_] is not known to comply with the type bound >: Foo.

So I was able to fix it by providing a type parameter that is known to comply with the type bound:

def bar[T >: Foo](b: Bar[T]): Nothing = foo(b)

Not sure why it complies with the bound written this way, but not the way you wrote (I’ve never seen the ? syntax before). I’m sure someone knows.

In my original code, I’m not sure I can get rid of the question mark. In fact, the compiler seems to be inserting it all by itself, due to variance.

Please submit an issue. It might be a bug. I can investigate later this week but I won’t remember if there’s no issue.

1 Like

I don’t know what ? does, but can you share the code? Is it too big and complicated?

Here is the part of the code where I originally encountered the issue. It can be reproduced by uncommenting loopExample2 and building the project. But investigating it is probably not worth your effort. The code might be hard to follow, and is very much a work in progress.

? represents an unspecified type (formerly _). See the documentation here and here.

1 Like

Here is the issue.