RFR: 8317993: Add capturing factories to classes in java.util.function package
ExE Boss
duke at openjdk.org
Tue Oct 17 12:22:33 UTC 2023
On Tue, 17 Oct 2023 08:05:09 GMT, Per Minborg <pminborg at openjdk.org> wrote:
> This PR proposes to add a number of "capturing factories" in classes in the `java.util.function` package.
>
> The PR additionally (an optionally) proposes to add a new function `UnaryOperator::andThenUnary` to allow composition while retaining the `UnaryOperator` type.
>
> With the new changes, it is possible to write code like this (example from `java.util.function.Function`):
>
>
> // Resolve ambiguity
> var function = Function.of(String::isEmpty); // Function<String, Boolean>
> var predicate = Predicate.of(String::isEmpty); // Predicate<String>
>
> // Fluent composition
> var chained = Function.of(String::length) // Function<String, Integer>
> .andThen(Integer::byteValue); // Function<String, Byte>
>
>
> Please see the original bug report for a comprehensive description of these proposed changes.
>
> Note: It is not the objective to promote `var` declaration or to prevent previous ways of capturing lambdas and method references. The comments in the code above is for explaining the binding and once that becomes obvious, such comments are likely to not appear in real code. Users that prefers having a normal type declaration can still do that.
>
> Note: Functional interfaces of primitives have not been considered (in this round). Otherwise, functional interfaces that might be ambiguous or that supports composition have been included. Hence, `Supplier` did not get a factory method.
The factory methods should probably use bounded wildcards.
src/java.base/share/classes/java/util/function/BiConsumer.java line 104:
> 102: * @param <U> the type of the second argument to the operation
> 103: */
> 104: static <T, U> BiConsumer<T, U> of(BiConsumer<T, U> uncaptured) {
Suggestion:
static <T, U> BiConsumer<T, U> of(BiConsumer<? super T, ? super U> uncaptured) {
src/java.base/share/classes/java/util/function/BiFunction.java line 94:
> 92: * @param <R> the type of the result of the function
> 93: */
> 94: static <T, U, R> BiFunction<T, U, R> of(BiFunction<T, U, R> uncaptured) {
Suggestion:
static <T, U, R> BiFunction<T, U, R> of(BiFunction<? super T, ? super U, ? extends R> uncaptured) {
src/java.base/share/classes/java/util/function/BiPredicate.java line 128:
> 126: * @param <U> the type of the second argument the predicate
> 127: */
> 128: static <T, U> BiPredicate<T, U> of(BiPredicate<T, U> uncaptured) {
Suggestion:
static <T, U> BiPredicate<T, U> of(BiPredicate<? super T, ? super U> uncaptured) {
src/java.base/share/classes/java/util/function/Consumer.java line 87:
> 85: * @param <T> the type of the input to the operation
> 86: */
> 87: static <T> Consumer<T> of(Consumer<T> uncaptured) {
Suggestion:
static <T> Consumer<T> of(Consumer<? super T> uncaptured) {
src/java.base/share/classes/java/util/function/Function.java line 121:
> 119: * @param <R> the type of the result of the function
> 120: */
> 121: static <T, R> Function<T, R> of(Function<T, R> uncaptured) {
Suggestion:
static <T, R> Function<T, R> of(Function<? super T, ? extends R> uncaptured) {
src/java.base/share/classes/java/util/function/Predicate.java line 160:
> 158: * @param <T> the type of the input to the predicate
> 159: */
> 160: static <T> Predicate<T> of(Predicate<T> uncaptured) {
Suggestion:
static <T> Predicate<T> of(Predicate<? super T> uncaptured) {
-------------
PR Review: https://git.openjdk.org/jdk/pull/16213#pullrequestreview-1682167914
PR Review Comment: https://git.openjdk.org/jdk/pull/16213#discussion_r1362019934
PR Review Comment: https://git.openjdk.org/jdk/pull/16213#discussion_r1362020142
PR Review Comment: https://git.openjdk.org/jdk/pull/16213#discussion_r1362020330
PR Review Comment: https://git.openjdk.org/jdk/pull/16213#discussion_r1362020494
PR Review Comment: https://git.openjdk.org/jdk/pull/16213#discussion_r1362020680
PR Review Comment: https://git.openjdk.org/jdk/pull/16213#discussion_r1362020826
More information about the core-libs-dev
mailing list