Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8322003

JShell - Incorrect type inference in lists of records implementing interfaces

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 23
    • 17, 21, 22
    • tools
    • Observed in 17, 21 and 22 build 22-ea+27-2262.

    • b05

    Backports

      Description

        A list of records that implement a common Shape interface is inferred as a List<Record &Shape>.
        When its elements are passed to a Consumer<Shape> in JShell, the following error is displayed:
        Error:
        | incompatible types: java.util.function.Consumer<Shape> cannot be converted to java.util.function.Consumer<? super java.lang.Record>
        This error is not there in a classical main method.
        The following code shows this bug.
        ```
        interface Shape {}
        record Square(int edge) implements Shape {}
        record Circle(int radius) implements Shape {}
        Consumer<Shape> printShape = System.out::println;
        Square square = new Square(1);
        Circle circle = new Circle(1);
        var shapes = List.of(square, circle);
        shapes.forEach(printShape);
        ```

        Attachments

          Issue Links

            Activity

              People

                jlahoda Jan Lahoda
                jpaumard Jose PAUMARD
                Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: