JEP 323: Local-Variable Syntax for Lambda Parameters
Summary
Allow var
to be used when declaring the formal parameters of implicitly typed lambda expressions.
Goals
- Align the syntax of a formal parameter declaration in an implicitly typed lambda expression with the syntax of a local variable declaration.
Non-goals
- Align the syntax of any other kind of variable declaration, e.g., a formal parameter of a method, with the syntax of a local variable declaration.
Motivation
A lambda expression may be implicitly typed, where the types of all its formal parameters are inferred:
(x, y) -> x.process(y) // implicitly typed lambda expression
Java SE 10 makes implicit typing available for local variables:
var x = new Foo();
for (var x : xs) { ... }
try (var x = ...) { ... } catch ...
For uniformity with local variables, we wish to allow 'var' for the formal parameters of an implicitly typed lambda expression:
(var x, var y) -> x.process(y) // implicit typed lambda expression
One benefit of uniformity is that modifiers, notably annotations, can be applied to local variables and lambda formals without losing brevity:
@Nonnull var x = new Foo();
(@Nonnull var x, @Nullable var y) -> x.process(y)