Skip to content

Bad type inference for java stream's toArray #13123

@He-Pin

Description

@He-Pin

Reproduction steps

  • works in Scala 3.3.6
  • bad in Scala 2.13.16
  private val CLASS_STACK_WALKER: java.util.function.Function[
    java.util.stream.Stream[StackWalker.StackFrame], Array[Class[_]]] =
    (frames: java.util.stream.Stream[StackWalker.StackFrame]) =>
      frames.map(frame => frame.getDeclaringClass)
        .toArray((size: Int) => new Array[Class[_]](size))

Problem

type mismatch;
[error]  found   : Array[Class[_]]
[error]  required: Array[Class[Nothing]]
[error] Note: Class[_] >: Class[Nothing], but class Array is invariant in type T.
[error] You may wish to investigate a wildcard type such as `_ >: Class[Nothing]`. (SLS 3.2.10)
[error]         .toArray((size: Int) => new Array[Class[_]](size))

How to fix:

Add the [Class[_]]

  private val CLASS_STACK_WALKER: java.util.function.Function[
    java.util.stream.Stream[StackWalker.StackFrame], Array[Class[_]]] =
    (frames: java.util.stream.Stream[StackWalker.StackFrame]) =>
      frames.map(frame => frame.getDeclaringClass)
        .toArray[Class[_]]((size: Int) => new Array[Class[_]](size))

apache/pekko#2260

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions