8

In Function.class from Java8, we have:

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
    Objects.requireNonNull(before);
    return (V v) -> apply(before.apply(v));
}

Compose accepts:

Function<? super V, ? extends T> before

Rather than:

Function<V, ? extends T> before

Is there any plausible situation in which the fact that "V" is lower bounded matters?

1
  • 2
    Function interface is uses in cases where you want to encapsulate some code into a method which accepts some value as an input parameter and then returns another value after performing required operations on the input. The input parameter type and the return type of the method can either be same or different which makes sense
    – bananas
    Jul 16, 2016 at 6:49

1 Answer 1

5

The ? super allows the returned Function's input type (V) to be different from the arguments input type.

For example, this compiles with the ? super version but not the alternate one.

Function<Object, String> before = Object::toString;
Function<String, Integer> after = Integer::parseInt;
Function<Integer, Integer> composed = after.compose(before);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.