Re: new Java lambda syntax
On 9/11/2011 2:14 PM, Steven Simpson wrote:
On 11/09/11 19:08, BGB wrote:
I would have also liked to see lexical variable capture.
FFS, I added this (along with closures) to a C compiler before, can't
be too hard
At this stage, I don't think the issue is how, but whether/when to
permit it.
There are other issues like does it capture the value or does it use the
same variable. e.g., what would this produce:
List<Runnable> runners = new LinkedList<Runnable>();
for (int i = 0; i < 10; i++) {
runners.add(() => { System.out.println("Value of i is " + i); });
}
for (Runnable r : runners) {
r.run();
}
Should you see 0..9 or 10 repeated 10 times?
For invocations, having to type obj.run() instead of obj() is hardly
onerous. Plus, invocations will be much rarer than lambda declarations.
Also note that the invocation site is unaware of whether the object is a
lambda.
Also, note the (slight) benefits of explicitly saying what you are
doing. You might choose, reasonably, to call the callback parameter for
an asyncForEach function `block', at which point the functional call
specification becomes block(value), which can be visually ambiguous as
to what it's doing. block.call(value) is clearer, on the other hand.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth