Summary
Enhance the Java Language to extend type inference to declarations of local variables with initializers.
Problem
Declarations of the types of local variables are often unnecessary. See JEP 286 for an extended discussion.
Solution
The identifier var
is removed from the domain of valid type names, and re-interpreted in local variable declarations, for
loops, and try
-with-resources statements as an indication that the type of the variable should be inferred.
Inference determines the type of the variable's initializer, as if it were a standalone expression. A sanitization step removes any capture variables from the type by performing an upward projection to a supertype. The result is the type of the variable.
An error occurs if the initializer has the null type, or is a poly expression that requires a target type. An error also occurs if the declaration has an unsupported form (such as a local variable declaration with trailing array dimensions).
Specification
Proposed changes to the Java Language Specification are attached.
- blocks
-
JDK-8151454 JEP 286: Local-Variable Type Inference
-
- Closed
-
- csr of
-
JDK-8177466 Add compiler support for local variable type-inference
-
- Resolved
-