When a Java method accepts a Function<? super T, ? extends U>, then it's possible to provide method references in a syntax like the following: MyClass::myMethod.
However, I'm wondering if there's a way to chain multiple method calls. Here's an example to illustrate the case.
// on a specific object, without lambda
myString.trim().toUpperCase()
I am wondering if there is a syntax to translate this to a lambda expression. I am hoping there is something like the following:
// something like: (which doesn't work)
String::trim::toUpperCase
Alternatively, is there a utility class to merge functions?
// example: (which does not exist)
FunctionUtil.chain(String::trim, String::toUpperCase);
myString -> myString.trim().toUpperCase().a.f().g() == (a.f()).g(). We can nest::too:)::operator. Sincex::yis an expression, the form(x::y)::zdenote the form expression::name. Unfortunately, the left-hand side has no type, so it doesn’t work, but if it had a type, it was a function type as the expressionx::yproduces a function. Thus,zwould be searched within the function type which is not what you want as you want to havezbe searched within the result type. But there is no point in changing the way expression nesting works, as there is already a way to denote what you want:x->y().z().((Function)x::y)::applywould be valid, but not useful. It does not create a function chain. It would be useful ifFunctionhad astaticmethod, saychain, accepting twoFunctions so that you could writeFunction.chain(x::y, r::z)whereasris the result type ofy(). However, it’s still no advantage overx->x.y().z()…