I noticed that this Java 17 code snippet doesn't compile.
class Foo<T> {
public Optional<T> getSomeOptional() {
return Optional.empty();
}
}
class Bar {
List<Foo<?>> fooList = new ArrayList<>();
void bar() {
List<Optional<?>> list = fooList.stream().map(Foo::getSomeOptional).toList();
}
}
but changing class Bar to this works.
class Bar {
List<Foo<?>> fooList = new ArrayList<>();
void bar() {
List<? extends Optional<?>> list = fooList.stream().map(Foo::getSomeOptional).toList();
}
}
Why is ? extends required here in the declaration?