Search code examples
scalagenericsexistential-type

Why does usage of Foo[T <: Bar] require Foo[_ <: Bar] rather than Foo[_]


Given

trait Foo[T <: Bar] {
   def foo: T
}

why does the compiler NOT interpret signature like

def foo(f: Foo[_])

to mean that the unknown _ must be a type of Bar. Instead, it forces me to do this

def foo(f: Foo[_ <: Bar])

which complicates signatures. I'm sure the compiler must be right and I'm missing some subtlety but clearly a Foo[X] where X is NOT some kind of Bar so not possible by construction, so why?

Thank you.


Solution

  • In short - seems to be a deficiency of the compiler and an open question whether to address it. Fixing it may have unintended consequences but it's been raised often enough to still be considered seriously as a possible fix. Here are some of the most relevant and still open Bugs tickets.

    P.S. It seems that Dotty does honor such bounds so they may well fix it!