Found this answer from smarks:

https://stackoverflow.com/questions/26549659/built-in-java-8-predicate-that-always-returns-true/26553481#26553481

Certainly there is some value in something that has a name vs. a
> written-out lambda, but this value is quite small. We expect that people
> will learn how to read and write x -> true and () -> { } and that their
> usage will become idiomatic. Even the value of Function.identity() over x
> -> x is questionable.


Makes sense. And where would we stop..?

EIrik.

On Mon, Feb 20, 2023 at 7:24 PM Eirik Bjørsnøs <eir...@gmail.com> wrote:

> Hi,
>
> So I found myself writing some code which sets up a j.u.s.Stream
> processing pipeline where the stream can optionally be traced/logged via a
> peeking Consumer.
>
> The default should be not to trace, in which case I initially set the
> Consumer to null. But then I need to check that trace != null when setting
> up the stream. This felt awkward..
>
> Since related code was using Function.identity() as a "no mapping
> configured" default, I went looking for Consumer.empty(), only to learn
> that no such thing exists.
>
> I can of course just do () -> {}, but I think Consumer.empty() conveys the
> purpose a bit more clearly..
>
> Is there some thought behind this not existing? Would it be worth adding?
>
>
> /**
>  * Returns a consumer which performs no operation on its input argument
>  *
>  * @param <T> the type of the input argument
>  * @return a Consumer which performs no operation on its input argument
>  */
> static <T> Consumer<T> empty() {
>     return t -> {};
> }
>

Reply via email to