Question about latest binary build
Dan Smith
daniel.smith at oracle.com
Thu Jul 19 10:47:53 PDT 2012
On Jul 18, 2012, at 10:53 AM, Marcus Thiesen wrote:
> 2012/7/18 Richard Warburton <richard.warburton at gmail.com>:
>> This issue being confusing for developers came up pretty frequently at
>> the LJC Lambda Hackdays.
>
> Well, I figured that, but I thought that there might be a good reason
> behind that other than confusing people?
I think confusing people is the main motivation. Got to keep the book publishers employed. :-)
Actually, the rules have been relaxed, and your example should work, once the change is implemented in the compiler.
Lambda bodies have two forms: an expression or a block. The block form is just like a method body. The expression form simply evaluates the expression on invocation. Void method invocations don't produce anything, so it seemed somewhat awkward to treat them the same as value-producing expressions, but the rules were relaxed in the most recent EDR so that a void method invocation can appear as the body of a lambda that targets a void-returning functional interface.
Runnable r = () -> System.out.println("hi");
Thread t = new Thread(() -> System.out.println("hi")); // note the placement of ';' -- it's not part of the lambda
Still on the to-do list is whether we want to allow value-producing expressions to be compatible with void-returning functional interfaces:
Runnable r = () -> list.add("hi");
Seems convenient, but causes trouble for overload resolution. So for now, this is not allowed (pending further discussion). You would need to use a block instead:
Runnable r = () -> { list.add("hi"); };
—Dan
More information about the lambda-dev
mailing list