Skip to content

An Array cannot be used in place of varargs with type parameters. #4003

Open
@scabug

Description

@scabug

This happens when vararg type has type parameters.

Both Array and mutable.Seq are invariant. However using Array as an argument in vararg function fails, while mutable.Seq works.

object Main {
  def main(args: Array[String]) {
    bar(new B)
  }

  def bar(b: B) {
    val array = Array(b, b)
    val seq :scala.collection.mutable.Seq[B] = array

    foo(b, b)
    foo(seq: _*) //works
    foo(array: _*) //compiler error
  }

  def foo(s: A[_]*) {
    s.foreach(println)
  }
}

class A[T]
class B extends A[String]

Compiler error:

error: type mismatch;
 found   : Array[B]
 required: Array[A[?>: Nothing <: Any]]
    foo(array: _*)
one error found

Everything works when vararg type does not have any type parameters:

object Main {
  def main(args: Array[String]) {
    bar(new B)
  }

  def bar(b: B) {
    val array = Array(b, b)
    val seq :scala.collection.mutable.Seq[B] = array

    foo(b, b)
    foo(seq: _*)
    foo(array: _*)
  }

  def foo[T](s: A*) {
    s.foreach(println)
  }
}

class A
class B extends A

Tested on 2.8.1.final and 2.9.0.r23541.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions