Draft JEP: Unnamed local variables and patterns
Till Brychcy
till.brychcy at unite.eu
Tue Oct 18 06:43:18 UTC 2022
> 7. a resource specification of a try-with-resources statement
>
> Today we get this warning with a TWR-statement if the local is ignored:
>
> >
> javac -Xlint:all MyScopedLock.java
>
> MyScopedLock.java:23: warning: [try] auto-closeable resource ignored is never referenced in body of corresponding try statement
> try(MyScopedLock ignored = l.lock()) {
> ^
> 1 warning
>
> So here we may have a clash of philosophies for the enhanced-for and how people
> use the `AutoCloseable`.
We have a similar use case: a logging framework that uses the close() call to measure the runtime of some code block.
It looks like this:
try (__ __ = ThreadLogger.block(SomeClass.class, "someAction")) {
// some code
}
Note we named the helper class and variable already "__" to make clear that they are not used - before Java 8 we used to called them "_“ :-)
Big advantages in comparison to e.g. a lambda based solution are that this doesn’t interfere with exceptions and variables outside the try statement can be assigned.
There are also more cases reported by other people in https://bugs.eclipse.org/bugs/show_bug.cgi?id=560733 , which why an already implemented warning for such variables by Eclipse was removed again.
Wir stellen uns als Marke neu auf und stehen mehr denn je zusammen.
Unite ist nun unsere führende Unternehmensmarke.
Erfahren Sie mehr
Folgen Sie uns auf LinkedIn, Twitter oder YouTube
unite.eu
Unite Services GmbH & Co. KG, im Auftrag von Unite
Unite Network SE
Neumarkt 9, 04109 Leipzig, Deutschland | Amtsgericht Leipzig, HRB 39278
Vorstand: Dr. Sebastian Wieser (Vorsitzender), Christel Constant, Peter Ledermann, Dr. Bernd Schönwälder
Aufsichtsrat: Karl Mayer-Rieckh (Vorsitzender)
Mercateo Deutschland AG
Mercateo Deutschland AG Fürstenfelder Straße 5, 80331 München, Deutschland | Amtsgericht München, HRB 243681
Vorstand: Dr. Sebastian Wieser (Vorsitzender), Christel Constant, Peter Ledermann, Dr. Bernd Schönwälder
Aufsichtsrat: Karl Mayer-Rieckh (Vorsitzender)
mercateo.com/procure
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-comments/attachments/20221018/40da93cc/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image985586.png
Type: image/png
Size: 4303 bytes
Desc: image985586.png
URL: <https://mail.openjdk.org/pipermail/amber-spec-comments/attachments/20221018/40da93cc/image985586-0001.png>
More information about the amber-spec-comments
mailing list