Skip to content

fix handing of the java type T[] #533

Closed
@smarter

Description

@smarter

JA.java:

class JA {
  public static <T> T get(T[] arr) {
    return arr[0];
  }
}

Compat.scala:

object Compat {
  def main(args: Array[String]): Unit = {
    val x = new Array[Int](1)
    x(0) = 10
    println(JA.get(x))
  }
}

This should output an error since T[] is erased to Object[] but it compiles fine, for comparison, scalac will complain with:

Compat.scala:5: error: type mismatch;
 found   : Array[Int]
 required: Array[? with Object]
Note: Int >: ? with Object, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: ? with Object`. (SLS 3.2.10)
    println(JA.get(x))
                   ^
one error found

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions