scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.73k stars 1.04k forks source link

Type inference of lambdas not working #11954

Open japgolly opened 3 years ago

japgolly commented 3 years ago

Note: Sorry for the poor title choice. I can't think of a concise way to describe this more accurately. Feel free to rename of course.

Compiler version

3.0.0-RC1

Minimized code

object X {

  class Component[P, S]
  trait Unmounted[P, M]
  trait Mounted[P, S]

  def unmounted[P, S, M](p: P, m: Component[P, S] => M): Unmounted[P, M] =
    ???

  def mounted[P, S](r: Component[P, S]): Mounted[P, S] =
    ???

  def test_KO[P, S](p: P): Unmounted[P, Mounted[P, S]] =
    unmounted(p, mounted) // error

  def test_OK[P, S](p: P): Unmounted[P, Mounted[P, S]] =
    unmounted(p, mounted[P, S]) // ok
}

Output

14 |    unmounted(p, mounted) // error
   |                 ^^^^^^^
   |                 Found:    X.Mounted[P, Any]
   |                 Required: X.Mounted[P, S]

Expectation

It should compile. Scala 2.x compiles without problem.

smarter commented 3 years ago

It's the usual limitations with type inference going through the body of a lambda, Scala 2 manages to get that example working but it fails if we just replace mounted by x => mounted(x), so it must be a pretty narrow special-case. I'm interested in solving the general problem (https://github.com/lampepfl/dotty/pull/9076), but less so in adding more special-cases, unless they're really common.