New candidate JEP: 443: Unnamed Patterns and Variables (Preview)
Brian Goetz
brian.goetz at oracle.com
Wed Mar 22 12:13:35 UTC 2023
On 3/21/2023 5:39 PM, Holo The Sage Wolf wrote:
>
> Is there a reason not to allow implicit Unnamed Variables in
> try-with-resources and Catch-block?
>
The JEP already describes these cases:
Unnamed variables
The following kinds of declarations can introduce either a named
variable (denoted by an identifier) or an unnamed variable (denoted by
an underscore):
* 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), and
* A formal parameter of a lambda expression (JLS 15.27.1).
Try with resources, and catch blocks, are listed as places where you can
use unnamed variables. Am I undersatnding your question properly?
> To allow:
>
> |try (Scope.open()) { ... } --- try { ... } catch (Exception) { ... } |
>
> to be implicitly
>
> |try (var _ = Scope.open()) { ... } --- try { ... } catch (Exception
> _) { ... } |
>
> Unlike a normal expression, and expression on the RHS of a
> try-with-resources variable has an explicit context, there is no fear
> of someone forgetting the result of the expression by mistake.
>
> In a Catch block the exception is uniquely accessible through the
> variable declared in the declaration of the block, so there is no fear
> that someone will get confused by this implicitness.
>
> This implicitness will make stuff like logging context, which is far
> from a niche use case, much more elegant.
>
>
> On Tue, Mar 21, 2023 at 9:17 PM Mark Reinhold
> <mark.reinhold at oracle.com> wrote:
>
> https://openjdk.org/jeps/443
>
> Summary: Enhance the Java language with unnamed patterns, which
> match
> a record component without stating the component's name or type,
> and unnamed variables, which can be initialized but not used. Both
> are denoted by an underscore character, _. This is a preview
> language
> feature.
>
> - Mark
>
>
>
> --
> Holo The Wise Wolf Of Yoitsu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20230322/2808a18d/attachment-0001.htm>
More information about the jdk-dev
mailing list