Why Optional's or and flatMap method's supplier type parameter is a wildcard
up vote
6
down vote
favorite
The Optional.or
method was added in Java 9. This is the method signature
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
Why is the type parameter of the Supplier
taking ? extends Optional
rather than just Optional
since Optional
is a final class.
The same is true for Optional.flatMap
method. This is a change from Java 8.
In Java 8, it was Function<? super T, Optional<U>> mapper
wherease it was changed to Function<? super T,? extends Optional<? extends U>>
in Java 9.
java optional java-9 supplier
add a comment |
up vote
6
down vote
favorite
The Optional.or
method was added in Java 9. This is the method signature
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
Why is the type parameter of the Supplier
taking ? extends Optional
rather than just Optional
since Optional
is a final class.
The same is true for Optional.flatMap
method. This is a change from Java 8.
In Java 8, it was Function<? super T, Optional<U>> mapper
wherease it was changed to Function<? super T,? extends Optional<? extends U>>
in Java 9.
java optional java-9 supplier
Kinda seems like they're positioning it to not be afinal
class in future version?
– nbrooks
4 hours ago
@nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
– user7
4 hours ago
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
The Optional.or
method was added in Java 9. This is the method signature
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
Why is the type parameter of the Supplier
taking ? extends Optional
rather than just Optional
since Optional
is a final class.
The same is true for Optional.flatMap
method. This is a change from Java 8.
In Java 8, it was Function<? super T, Optional<U>> mapper
wherease it was changed to Function<? super T,? extends Optional<? extends U>>
in Java 9.
java optional java-9 supplier
The Optional.or
method was added in Java 9. This is the method signature
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
Why is the type parameter of the Supplier
taking ? extends Optional
rather than just Optional
since Optional
is a final class.
The same is true for Optional.flatMap
method. This is a change from Java 8.
In Java 8, it was Function<? super T, Optional<U>> mapper
wherease it was changed to Function<? super T,? extends Optional<? extends U>>
in Java 9.
java optional java-9 supplier
java optional java-9 supplier
edited 3 hours ago
nullpointer
38k1073145
38k1073145
asked 5 hours ago
user7
8,89132040
8,89132040
Kinda seems like they're positioning it to not be afinal
class in future version?
– nbrooks
4 hours ago
@nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
– user7
4 hours ago
add a comment |
Kinda seems like they're positioning it to not be afinal
class in future version?
– nbrooks
4 hours ago
@nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
– user7
4 hours ago
Kinda seems like they're positioning it to not be a
final
class in future version?– nbrooks
4 hours ago
Kinda seems like they're positioning it to not be a
final
class in future version?– nbrooks
4 hours ago
@nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
– user7
4 hours ago
@nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
– user7
4 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
9
down vote
I found the reasoning behind this from Stuart Marks himself
http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html
This has to do with nested generics (Optional
is nested within Function
).
From the mail thread
Function<..., Optional<StringBuilder>>
is not a subtype of
Function<..., Optional<? extends CharSequence>>
To get around this, we have to add the outer wildcard as well, so that
Function<..., Optional<StringBuilder>>
is a subtype of
Function<..., ? extends Optional<? extends CharSequence>>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
I found the reasoning behind this from Stuart Marks himself
http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html
This has to do with nested generics (Optional
is nested within Function
).
From the mail thread
Function<..., Optional<StringBuilder>>
is not a subtype of
Function<..., Optional<? extends CharSequence>>
To get around this, we have to add the outer wildcard as well, so that
Function<..., Optional<StringBuilder>>
is a subtype of
Function<..., ? extends Optional<? extends CharSequence>>
add a comment |
up vote
9
down vote
I found the reasoning behind this from Stuart Marks himself
http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html
This has to do with nested generics (Optional
is nested within Function
).
From the mail thread
Function<..., Optional<StringBuilder>>
is not a subtype of
Function<..., Optional<? extends CharSequence>>
To get around this, we have to add the outer wildcard as well, so that
Function<..., Optional<StringBuilder>>
is a subtype of
Function<..., ? extends Optional<? extends CharSequence>>
add a comment |
up vote
9
down vote
up vote
9
down vote
I found the reasoning behind this from Stuart Marks himself
http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html
This has to do with nested generics (Optional
is nested within Function
).
From the mail thread
Function<..., Optional<StringBuilder>>
is not a subtype of
Function<..., Optional<? extends CharSequence>>
To get around this, we have to add the outer wildcard as well, so that
Function<..., Optional<StringBuilder>>
is a subtype of
Function<..., ? extends Optional<? extends CharSequence>>
I found the reasoning behind this from Stuart Marks himself
http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-October/044026.html
This has to do with nested generics (Optional
is nested within Function
).
From the mail thread
Function<..., Optional<StringBuilder>>
is not a subtype of
Function<..., Optional<? extends CharSequence>>
To get around this, we have to add the outer wildcard as well, so that
Function<..., Optional<StringBuilder>>
is a subtype of
Function<..., ? extends Optional<? extends CharSequence>>
answered 4 hours ago
user7
8,89132040
8,89132040
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53698401%2fwhy-optionals-or-and-flatmap-methods-supplier-type-parameter-is-a-wildcard%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Kinda seems like they're positioning it to not be a
final
class in future version?– nbrooks
4 hours ago
@nbrooks It is because of the nested generics (and yes.. partly to due with the future changes)
– user7
4 hours ago