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?

Noozen
  • 379
  • 3
  • 13
  • 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 '16 at 06:49

1 Answers1

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);
Alex - GlassEditor.com
  • 14,957
  • 5
  • 49
  • 49