view raw
stacker stacker - 9 months ago 50
Java Question

What does this syntax a -> b -> a + b; produce?

This answer does-java-support-currying defines a

curriedAdd = a -> b -> a + b;

IntFunction<IntUnaryOperator> curriedAdd = a -> b -> a + b;
IntUnaryOperator adder5 = curriedAdd.apply(5);

I read the relevant section 15.27 of the JLS it states:

The production of a lambda expression is
LambdaExpression: LambdaParameters -> LambdaBody

Does this mean that
a -> b
is one lamba expression and
b -> a + b
another one ?


You can expand the expression to make it easier to understand:

IntFunction<IntUnaryOperator> curriedAdd = a -> { return b -> a + b; };

And indeed you have two lambdas expressions: the inner one which is the unary operator and the outer one which is the function.