Hello,

I've recently learned about JDK 12's new String::transform method:
https://bugs.openjdk.java.net/browse/JDK-8203703

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
types.

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
dismissed:

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

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?

Thanks,
Lukas

[1]:
https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/
[2]: https://hackernoon.com/operator-in-scala-cbca7b939fc0
[3]: https://github.com/tc39/proposal-pipeline-operator

Reply via email to