In Java 8, the class java.util.Optional
(javadoc) class offers functionality of the "Maybe" Monad or the Option Type.
More directly:
public final class java.util.Optional<T> extends Object
A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.
<U> Optional<U> map(Function<? super T,? extends U> mapper)
If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result.
The question is simply why does map()
use a type wildcard on the U
type.
My understanding of type wildcarding is simply that:
? super T
designates some type from the set of classes given by the subclassing path from Object
to T
(both Object
and T
included) union the set of interfaces found on any subinterfacing path pulled in "as a sidedish" via implements
.
And ? extends U
simply designates some type from the set of classes that extend U
(U
included).
So one could just write:
<U> Optional<U> map(Function<? super T,U> mapper)
without loss of information.
Or not?