On Tue, 17 Oct 2023 08:05:09 GMT, Per Minborg <pminb...@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.

src/java.base/share/classes/java/util/function/BiConsumer.java line 78:

> 76:     /**
> 77:      * {@return a representation of the provided {@code uncaptured} 
> lambda or method reference
> 78:      * in the form of a {@code BiConsumer}}

There's no need for the provided arguments to this and the other "of" methods 
to be a lambda or a method reference expression, although it is true all that 
the methods are useful especially for such cases.

src/java.base/share/classes/java/util/function/BiConsumer.java line 105:

> 103:      */
> 104:     static <T, U> BiConsumer<T, U> of(BiConsumer<T, U> uncaptured) {
> 105:         return Objects.requireNonNull(uncaptured);

This does more than just capturing: not sure if the `null` check is generally 
useful here and elsewhere.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/16213#discussion_r1362091242
PR Review Comment: https://git.openjdk.org/jdk/pull/16213#discussion_r1362105477

Reply via email to