Skip to content

Cannot assign nullable type parameter to generic types from Java under explicit nulls #24770

@HarrisL2

Description

@HarrisL2

Compiler version

3.8.0

Minimized code

J.java

import java.util.*;

public class J {
    public List<String> listS(String s) {
        return new ArrayList<T>();
    }
}

S.scala

import java.util.List

class S {
  def LList(s: String): List[String | Null] = (new J).listS("") // error
}

scalac local/J.java local/S.scala -Yexplicit-nulls

Output

-- [E007] Type Mismatch Error: local/S.scala:4:59 ------------------------------
4 |  def LList(s: String): List[String | Null] = (new J).listS("") // error
  |                                              ^^^^^^^^^^^^^^^^^
  |                                   Found:    (java.util.List[String])?
  |                                   Required: java.util.List[String | Null]
  |
  | longer explanation available when compiling with `-explain`

Expectation

It should be allowed, since the List actually does contain nullable Strings.

Even though the methods of List will be nullified at the call site, this example can extend to other generic Java types where it may even be necessary for the type parameter to be explicitly nullable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    itype:bugstat:needs triageEvery issue needs to have an "area" and "itype" label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions