flkes flkes - 2 years ago 117
Java Question

Lambda casting rules

I was curious why a lambda with a return type can not be casted to a

whereas a non void method reference can.

Runnable r1 = () -> 1; // not allowed
// error: incompatible types: bad return type in lambda expression
// int cannot be converted to void

Runnable r2 = ((Supplier)() -> 1)::get; // allowed

Answer Source

The Runnable interface defines the run method with return type void. In a lambda expression that means that the part following the arrow -> must be a statement. This is explained in JLS §15.27.3:

If the function type's result is void, the lambda body is either a statement expression (§14.8) or a void-compatible block.

The JLS $14.5 clearly defines the syntax of a statement. As explained above it must be an "ExpressionStatement" (§ 14.8). Looking there, you can find that a simple literal is not an adequate expression, but a method invocation is (even if it returns something).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download