<html><body><div dir="ltr">
    Lombok’s <code style="border:1px solid rgb(206,206,206);background-color:rgb(244,244,244);padding:0px 2px;border-radius:2px">@Cleanup</code> (<a href="https://projectlombok.org/features/Cleanup">https://projectlombok.org/features/Cleanup</a>) works exactly like that:<br><br></div><div dir="ltr"><div><pre style="border:1px solid rgb(206,206,206);background-color:rgb(244,244,244);padding:10px;border-radius:2px;margin-top:0px;margin-bottom:0px" dir="ltr">@Cleanup var foo1 = new Foo1();
@Cleanup var foo2 = new Foo2();
</pre></div></div><div dir="ltr"><div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr">Does what you want. However, it runs to the end of the scope that foo1 is defined in. An exotic java feature is that you can just write braces whenever you feel like, so you can use that to scope your resources as desired:</div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr"><br></div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr"><div><pre style="border:1px solid rgb(206,206,206);background-color:rgb(244,244,244);padding:10px;border-radius:2px;margin-top:0px;margin-bottom:0px" dir="ltr">void example() {
  @Cleanup var db = ...; // scoped for the method, so closed at the end
  
  String acctName; { // note, just an opening brace here, on its own. Legal.
    @Cleanup var rs = db.query(...);
    acctName = rs.getString(1);
  }
  
  System.out.println(acctName);
}</pre></div></div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr">It feels like the road not taken: That looks fine, is as flexible as you need it to be (in that you control exactly where the resource should be closed) and works great in daily use. It’s just… not what the try-with-resources construct ended up doing. It feels a little odd to add it <i>now</i>, and ‘just write some braces to scope some variables’ is not a thing your average java programmer ever does. I’m not quite sure why not (it’s quite useful given that java doesn’t make it easy to put helper methods inside methods), but, it is what it is: Introducing this feature more or less demands of the community that they get into the habit of writing braces solely to define a scope. This is a big ordeal; for example, most autoformatters will make this unduly ugly by forcibly trying to let the opening brace start on its own line (sometimes, it’s own entire paragraph, i.e. 2 newlines are injected), even though often it is more convenient and easier to read to open it <i>after</i> the relevant variable declaration, or a comment or label explaining what the point of this block is.</div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr"><br></div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr">A casual analysis indicates that going all out leads to nice results and anything in between would incur significant costs. In other words, we should have <i>either</i> a java where everybody is used to using braces to create scoped blocks and the <i>only</i> variant of try-with is the stand-alone one as you propose - <i>or</i> we have the java we have today, where almost nobody knows it’s legal to just write braces to create a scoped block, auto-formatters don’t know what to do with them, and try-with comes with its own mandatory block.</div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr"><br></div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr">Given that at this juncture it’s not possible to travel back in time and get rid of the existing try-with (with the mandatory block), I’d say: This idea doesn’t “work” for java. And that’s coming from someone who is so convinced your way is better, he programmed it into lombok about a year before try-with was introduced :)</div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr"><br></div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr">In other words, better is great, but ‘does not demand significant cultural adjustments and does not lead to endless style wars’ is better.</div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr"><br></div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr">The costs incurred are high cultural friction, style wars, and likely, that this alternate ‘form’ of try-with will be so rarely used, it will end up being a member of that list of ’java lang features most programmers don’t <i>ever</i> use and cannot recognize’. It’s not a good idea for that list to grow any longer than it already is.</div><div class="gmail_signature" data-smartmail="gmail_signature" dir="ltr"><br></div><div class="gmail_signature" data-smartmail="gmail_signature"> --Reinier Zwitserloot<br></div></div><br>
</div>
<br>
<div class="gmail_quote">
    <div dir="ltr" class="gmail_attr">On 27 Dec 2022 at 14:49:13, Red IO <<a href="mailto:redio.development@gmail.com">redio.development@gmail.com</a>> wrote:<br></div>
    <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" type="cite">
        <div dir="auto">I was once again writing closing heavy code in a java method and was wondering if I could reduce the clutter of the multi line try block:<div dir="auto">try (var foo1 = new Foo1() ;</div><div dir="auto"><span style="white-space:pre"> </span>var foo2 = new Foo2() ;</div><div dir="auto"><span style="white-space:pre">  </span>....) {</div><div dir="auto">... </div><div dir="auto">}</div><div dir="auto">Sometimes we need to initialize something between the resources and we get multiple levels of nesting. </div><div dir="auto">And I was wondering if we could not just lose the block and auto close at the end of a method like this :</div><div dir="auto"><br></div><div dir="auto">void foo() {</div><div dir="auto"><span style="white-space:pre">     </span>try (var foo1 = new Foo1());</div><div dir="auto"><span style="white-space:pre">     </span>//init some stuff </div><div dir="auto"><span style="white-space:pre">      </span>try (var foo2 = new Foo2());</div><div dir="auto"><span style="white-space:pre">     </span>// do some stuff</div><div dir="auto"><span style="white-space:pre"> </span>// auto close at end of method </div><div dir="auto">}</div><div dir="auto"><br></div><div dir="auto">I think method level try recourse would improve readability and unnecessary nesting significantly.</div></div>

    </blockquote>
</div></body></html>