Summary
-------
JEP 443: Unnamed Patterns and Variables introduces unnamed variables in Java. The `javax.lang.model` API need to be updated accordingly.
Problem
-------
JEP 443 repurposes the underscore token (`_`) to denote unnamed variables in Java in various locations:
- a local variable declaration statement in a block (JLS 14.4.2)
- a resource specification of a `try-with-resources` statement (JLS 14.20.3)
- the header of a basic for statement (JLS 14.14.1)
- the header of an enhanced `for` loop (JLS 14.14.2)
- an exception parameter of a `catch` block (JLS 14.20)
- a formal parameter of a lambda expression (JLS 15.27.1)
When any of the above variable elements is declared with the name `_`, they denote unnamed variables elements. Moreover, as they do not carry a distinguishable name, they cannot be referred throughout their enclosing scopes.
Solution
--------
We propose the following adjustments to the javax.lang.model API:
- Modify the method `javax.lang.model.element.VariableElement::getSimpleName()` to return empty name in the case of unnamed variables, making it consistent with unnamed packages, unnamed modules and anonymous classes. The `com.sun.source.Tree` API also returns an empty name for its version of the name through `com.sun.source.tree.VariableTree.getName()`.
- Add a new predicate method `javax.lang.model.element.VariableElement::isUnnamed()`. The default implementation of this method examines whether `getSimpleName()` returns an empty name.
Specification
-------------
The proposed API enhancements are attached as `specdiff.preliminary.00.zip`. These include the changes in javax/lang/model.
The changes to the specification and API are a subject of change until the CSR is finalized.