JDK 12's String::transform vs F#'s |> operator

Lukas Eder lukas.eder at gmail.com
Fri Mar 8 20:47:38 UTC 2019


I've recently learned about JDK 12's new String::transform method:

Obviously, this is useful. And obviously, this would be far more useful as
a general pattern than just something for String. E.g. why not also for any
Number subtype. For Boolean. For java.time types. Etc.

So, I'm wondering if this is a good opportunity to discuss about more
generally useful language features that could do the same thing but for all

I'm guessing that after Java 8's decision against extension methods and in
favour of default methods, the extension method approach will probably be

public "extension" <T, R> R T.transform(
    Function<? super T, ? extends R> f
) {

F# (and OCaml and some others) has a very useful operator |> [1]. F# also
has the inverse <| operator. It can be implemented relatively easily in
languages with tools like extension methods or implicit functions, such as
Scala [2], and it is being considered for JavaScript [3].

Before we see tons of new SomeFinalJDKType::transform methods being added
on an ad-hoc basis, would a pipeline operator be something useful and worth
discussing in Java, as well?


[2]: https://hackernoon.com/operator-in-scala-cbca7b939fc0
[3]: https://github.com/tc39/proposal-pipeline-operator

More information about the core-libs-dev mailing list