Generics type inference problem?

Attila Kelemen attila.kelemen85 at gmail.com
Sun Jun 11 16:16:49 UTC 2017


Hi,

I'm not sure if my issue is a violation of the specs or a bug, so I'm
writing here instead of a bug report. The following class does not compile
(imports are omitted) in Java 9 but does in Java 8:

    public final class NewClass {
        private static <E extends Throwable> List<TestFactory<E>> test(
                Collection<? extends Supplier<? extends E>> factory) {
            return factory.stream()
                    .map(NewClass::testMap)
                    .collect(Collectors.toList());
        }

        private static <E extends Throwable> TestFactory<E>
testMap(Supplier<? extends E> factory) {
            return null;
        }

        private static interface TestFactory<E extends Throwable> {
        }
    }

The above `test` method does not compile because type inference fails to
deduce the "appropriate" type for the return value of `Stream.collect`.
Which is strange given that `Stream.map` should map to
`Stream<TestFactory<E>>`. In fact, if I rewrite the method `test` to:

        Stream<TestFactory<E>> factoryStream =
factory.stream().map(NewClass::testMap);
        return factoryStream.collect(Collectors.toList());

The code will compile fine.

Is this behaviour in accordance with the spec or is this a bug?

The exact versions I have tried this with:

Java 8: java version "1.8.0_92", build 1.8.0_92-b14
Java 9: java version "9-ea", build 9-ea+170

Thanks,
Attila Kelemen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20170611/f1e4502d/attachment.html>


More information about the compiler-dev mailing list