Java 8 How to create functions without Lambdas?

I only saw examples for compiling functions (or people using lambdas).

Function<A,B> inner = ....; Function<B,C> outter = ....; Function<A,C> result = outter.compose(inner); 

I would like to compose the following functions using the "compose" function, and not just reference them directly.

 public class J{ public static B inner(final A a){...} public static C outter(final B b){...} } public class K{ public static Function<A,C> result = (J::outter).compose(J::inner); } 

This does not compile. I can't seem to use this "compound" member of java.util.function.Function. How to do this for traditionally declared functions? I would like to avoid the following:

 public class K{ public static Function<A,C> result = (a)-> J.outter(J.inner(a)); } 

Can this be done ??? thanks in advance

+4
source share
1 answer

Method references (and lambdas) need context to make sense in the source code. This context describes the target functional interface, provides details for type inference for generics, etc.

Using method references in

 public static Function<A,C> result = (J::outter).compose(J::inner); 

have no context. What should be the first J::outter ? Why?

Apply toss to give it this context

 public static Function<A, C> result = ((Function<B, C>) J::outter).compose(J::inner); 

which, in my opinion, is uglier than the lambda solution.

+8
source

All Articles