<div dir="auto">I think there where many valid points against it mentioned. But I disagree with your scope thesis. The "open try resource" is not meant to be used like this:<div dir="auto">void foo() {</div><div dir="auto"><span style="white-space:pre"> </span>..</div><div dir="auto"><span style="white-space:pre"> </span>{</div><div dir="auto"><span style="white-space:pre"> </span>try (..);</div><div dir="auto"><span style="white-space:pre"> </span>..</div><div dir="auto"><span style="white-space:pre"> </span>} </div><div dir="auto"><span style="white-space:pre"> </span>.. </div><div dir="auto">}</div><div dir="auto">Which is what I understand you mean by opening a scope just for the scope itself. It is meant primarily for use cases where your resource live the lifetime of the function, which is pretty common in well refractored code. Also adding the scope defeats the idea of the feature entirely. You could write:</div><div dir="auto">void foo() {</div><div dir="auto"> ..</div><div dir="auto">try (..) {</div><div dir="auto"> </div><div dir="auto"> ..</div><div dir="auto"> } </div><div dir="auto"> .. </div><div dir="auto">}</div><div dir="auto">Which is exactly what the current existing try resource is. The motivation of feature is to remove redundant nesting. You often encounter situations like this:</div><div dir="auto">void foo() {</div><div dir="auto"><span style="white-space:pre"> </span>try (..) {</div><div dir="auto"><span style="white-space:pre"> </span>//the entire code of the function </div><div dir="auto"><span style="white-space:pre"> </span>} </div><div dir="auto">}</div><div dir="auto">In this example the layer of nesting the try resource introduces is totally unnecessary since the scope is the same as the function scope. I think you underestimate the occurrences of the code above. Also with closable resources increasing in numbers the amount of nesting in Java code will be greatly increased. My estimate is that the scopeless try resource is as often applicable in Java as the scopeless if is.</div><div dir="auto">I proposed adding this feature not only because I think it's a great feature in general but also since it already has a track record of being useful and used by a community very similar to the java community. C# has the exact same feature as java's try resource just with a different keyword used (using). It has the scopeless version and if you look at some c# code that uses closable resources the scopeless variant is used 70+% of the time.</div><div dir="auto">It removes a lot of redundant scoping while not really adding much complexity nor using syntax that is likely to be used by some other feature in the future.</div><div dir="auto">I'm not sure how difficult the addition of a feature like this is since I still have no idea how changes in syntax are done in Java by any means. But if it's not to difficult to implement why not try it and ask the community what they think. I think asking the community whether they would use it or not is way more effective than speculating how popular it might be. It wouldn't even have to be implemented to ask the community what they think. I don't have the audience to make such surveys by any means. Otherwise I would do a post, video or poll myself. </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 27, 2022, 20:15 Reinier Zwitserloot <<a href="mailto:reinier@zwitserloot.com">reinier@zwitserloot.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><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" target="_blank" rel="noreferrer">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 data-smartmail="gmail_signature"><br></div><div 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 data-smartmail="gmail_signature" dir="ltr"><br></div><div 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 data-smartmail="gmail_signature"><br></div><div 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 data-smartmail="gmail_signature" dir="ltr"><br></div><div 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 data-smartmail="gmail_signature" dir="ltr"><br></div><div 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 data-smartmail="gmail_signature" dir="ltr"><br></div><div 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 data-smartmail="gmail_signature" dir="ltr"><br></div><div 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 data-smartmail="gmail_signature" dir="ltr"><br></div><div 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" target="_blank" rel="noreferrer">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-wrap"> </span>var foo2 = new Foo2() ;</div><div dir="auto"><span style="white-space:pre-wrap"> </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-wrap"> </span>try (var foo1 = new Foo1());</div><div dir="auto"><span style="white-space:pre-wrap"> </span>//init some stuff </div><div dir="auto"><span style="white-space:pre-wrap"> </span>try (var foo2 = new Foo2());</div><div dir="auto"><span style="white-space:pre-wrap"> </span>// do some stuff</div><div dir="auto"><span style="white-space:pre-wrap"> </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></div>
</blockquote></div>