Skip to content

Worse type inference when writing (x: Foo[_ >: A]) as opposed to [A1 >: A](x: Foo[A1]) #10920

Open
@smarter

Description

@smarter

In the following code, bar1 and bar2 are equivalent:

trait Foo[A] {
  def up[A1 >: A]: Foo[A1]
  def bar1(x: Foo[_ >: A]): Foo[A]
  def bar2[A1 >: A](x: Foo[A1]): Foo[A]
}

object Test {
  def test(a: Foo[Int], b: Foo[Any]) = {
    val x1 = a.up.bar1(b)
    val x2 = a.up.bar2(b)
  }
}

However, type inference works better for bar2 (in Dotty, both are inferred to Foo[Int]):

      val x1: Foo[Any] = a.up[Any].bar1(b);
      val x2: Foo[Int] = a.up[Int].bar2[Any](b);

It'd be good to figure out whether or not this can be fixed since this affects the API of the collections, in 2.13 we currently have in Seq:

def diff(that: Seq[_ >: A]): C

Which means that in the following code, x is inferred to have type Buffer[Any] and not Buffer[Int]:

object Test {
  def test(a: Seq[Int], b: Seq[Any]): Unit = {
    val x = a.toBuffer.diff(b)
  }
}

/cc @adriaanm @szeiger

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions