Skip to content

Java interop: Type Mismatch for vararg ...T because "Required: T & Object" #9439

Closed
@sjrd

Description

@sjrd

Minimized code

object Test {
  // First example with a concrete type <: AnyVal
  def main(args: Array[String]): Unit = {
    val coll = new java.util.ArrayList[Int]()
    java.util.Collections.addAll(coll, 5, 6)
    println(coll.size())
  }

  // Second example with an abstract type not known to be <: AnyRef
  def foo[A](a1: A, a2: A): Unit = {
    val coll = new java.util.ArrayList[A]()
    java.util.Collections.addAll(coll, a1, a2)
    println(coll.size())
  }
}

Output

-- [E007] Type Mismatch Error: tests\run\hello.scala:5:39 ----------------------
5 |    java.util.Collections.addAll(coll, 5, 6)
  |                                       ^
  |                                       Found:    (5 : Int)
  |                                       Required: Int & Object
-- [E007] Type Mismatch Error: tests\run\hello.scala:5:42 ----------------------
5 |    java.util.Collections.addAll(coll, 5, 6)
  |                                          ^
  |                                          Found:    (6 : Int)
  |                                          Required: Int & Object
-- [E007] Type Mismatch Error: tests\run\hello.scala:12:39 ---------------------
12 |    java.util.Collections.addAll(coll, a1, a2)
   |                                       ^^
   |                                       Found:    (a1 : A)
   |                                       Required: A & Object
-- [E007] Type Mismatch Error: tests\run\hello.scala:12:43 ---------------------
12 |    java.util.Collections.addAll(coll, a1, a2)
   |                                           ^^
   |                                           Found:    (a2 : A)
   |                                           Required: A & Object
4 errors found

Expectation

I expect that both examples compile.

Notes

JavaDoc of java.util.Collections.addAll

public static <T> boolean addAll(Collection<? super T> c, T... elements)

This prevents the Scala.js test CollectionsOnCollectionsTest from compiling, at the following line:
https://github.com/scala-js/scala-js/blob/6b9f04b0c0531d889235726d8956cb01a4c7476d/test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/CollectionsOnCollectionsTest.scala#L146

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions