3

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.

Creos
  • 2,445
  • 3
  • 27
  • 45
  • https://stackoverflow.com/questions/4323140/why-are-the-bounds-of-type-parameters-ignored-when-using-existential-types-in-sc – Dmytro Mitin Jun 13 '20 at 21:00
  • 1
    Thanks @DmytroMitin but the answer is from almost 10 years ago and it is a best-guess type of answer, would be nice to see if there is a more recent and concrete answer. Even the 2nd one with the link to the scala discussion thread itself is inconclusive. If moderators disagree I guess the question can be closed. Thanks for the thread though. – Creos Jun 13 '20 at 21:02
  • https://stackoverflow.com/questions/42696319/why-does-scala-require-existential-types-to-restrict-a-generic-bound – Dmytro Mitin Jun 13 '20 at 21:11
  • thanks @DmytroMitin i did a bit of investigation and i think my answer below is better than the answers in the two similar questions as it gives more up-to-date info and clearly states it looks like a legit compiler deficiency. it also confirms Dotty works as expected – Creos Jun 13 '20 at 21:42

1 Answers1

2

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!

Creos
  • 2,445
  • 3
  • 27
  • 45