RFR: 8015317: Optional.filter, map, and flatMap
Jed Wesley-Smith
jed at wesleysmith.io
Sat Jul 13 03:15:23 PDT 2013
> The ? extends Optional is unnecessary in flatMap as Optional is final.
interestingly enough, it actually is.
try the following test:
class OptionalTest {
class Parent {};
class Child extends Parent {};
@Test public void covariantReturn() {
Optional<Parent> some = some(new Parent());
Function<Parent, Optional<Child>> f = new Function<Parent,
Optional<Child>>() {
@Override public Optional<Child> apply(Parent p) {
return some(new Child());
}
};
Optional<Parent> mapped = some.<Parent> flatMap(f);
assertThat(mapped.get(), notNullValue());
}
}
adapted from the fugue test suite:
https://bitbucket.org/atlassian/fugue/src/96a65067fb7aaf1edae1bffa07167a5865cbebec/src/test/java/com/atlassian/fugue/OptionTest.java#cl-155
The point to remember is that Optional<Child> is a type and as such is
actually a subtype of Optional<Parent> – and therefore requires a
covariant return.
cheers,
jed.
On 13 July 2013 04:15, Mike Duigou <mike.duigou at oracle.com> wrote:
> The ? extends Optional is unnecessary in flatMap as Optional is final.
> Otherwise this looks good.
>
> Mike
>
> On Jul 5 2013, at 14:37 , Henry Jen wrote:
>
> > Hi,
> >
> > Please review the webrev at
> >
> > http://cr.openjdk.java.net/~henryjen/ccc/8015317.0/webrev/
> >
> > Which adds following method to Optional,
> >
> > public static <T> Optional<T> ofNullable(T value) {}
> > public Optional<T> filter(Predicate<? super T> predicate) {}
> > public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {}
> > public<U> Optional<U> flatMap(Function<? super T, ? extends Optional<U>>
> > mapper) {}
> >
> > Also included is some cleanup on javadoc.
> >
> > Cheers,
> > Henry
>
>
>
More information about the lambda-dev
mailing list