[lambda-leftovers] Lambda leftovers : state of the implementation
Vicente Romero
vicente.romero at oracle.com
Fri Mar 24 20:25:35 UTC 2017
Hi all,
The initial support for lambda left overs, JEP 302 [1], have been
recently pushed to the amber repo, see [2, 3, 4]. Below there is a
summary of the current state of the implementation,
Thanks,
Vicente
The JEP 302 covers three parts:
*
Better disambiguation for functional expression
*
Treatment of underscores
*
Shadowing of lambda parameters
The current implementation covers the last two of them. The fact of
allowing the use of underscore as an unnamed parameter implies that the
unnamed parameter has no scope, no name and can't be used for anything
but to declare a formal parameter with no name. The current
implementation forbids the use of the unnamed parameter out of it's
intended use. Most of the underscore related changes are at the parser
level as several productions have been changed. The following code shows
correct and incorrect use underscore:
import java.util.function.*;
public class UnderscoreTest {
void foo() {
BiFunction<Integer, String, String> biss1 =
(_, _) -> // this use is allowed
String.valueOf(_); // error '_' not in scope
// ok implicit lambda
BiFunction<Integer, String, String> biss2 =(_, _) -> "";
// ok explicit lambda
BiFunction<Integer, String, String> biss2 = (int _, String _)
-> "";
}
void bar() {
try { } catch (Throwable _) { // correct
throw _; // forbidden
}
}
void baz(String _) { // ok
System.out.println(_); // error '_' is not in scope
}
void m(String _[]) {} // error '_' can't be followed by dimensions
}
With regards to shadowing of lambda parameters, initial implementations
of lambdas allowed this feature. So restoring it has been pretty
straight ahead. The approach has been to use a 'fake' method symbol as
the owner of the lambda scope. The following shows the shadowing of
lambda parameters in action:
import java.util.*;
public class ShadowParameters {
void foo() {
Map<String, Integer> msi = null;
String key = "333";
msi.computeIfAbsent(key, key -> key.length());// no error as
the 'key' lambda parameter shadows the 'key' variable in the method's scope
}
}
[1] http://openjdk.java.net/jeps/302
[2] http://hg.openjdk.java.net/amber/amber/langtools/rev/2eef0bd5c274
[3] http://hg.openjdk.java.net/amber/amber/langtools/rev/077aa97c6b48
[4] http://hg.openjdk.java.net/amber/amber/langtools/rev/fdaad3f1a573
More information about the amber-dev
mailing list