<div dir="auto">A different approach is to give `yield` the same semantics as `break`, and interpreting `break label` as `yield label null`, this would make all label blocks be expressions.<div dir="auto"><br></div><div dir="auto">The only thing we need to be careful with is finally block, but this situation already happens with `return` inside of a finally block, so I don't think we need to handle this situation differently </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 5, 2023, 15:55 Tagir Valeev <<a href="mailto:amaembo@gmail.com" target="_blank" rel="noreferrer">amaembo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello!<div><br></div><div>Just for the record, three years ago I posted a similar, though more universal proposal, "do expressions":</div><div><a href="https://mail.openjdk.org/pipermail/amber-spec-experts/2020-March/002046.html" rel="noreferrer noreferrer" target="_blank">https://mail.openjdk.org/pipermail/amber-spec-experts/2020-March/002046.html</a><br></div><div><br></div><div>With best regards,</div><div>Tagir Valeev.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 5, 2023 at 11:58 AM Red IO <<a href="mailto:redio.development@gmail.com" rel="noreferrer noreferrer" target="_blank">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"><div dir="auto">I did some more research and testing and concluded that loop expressions are more difficult to achieve than I thought and are not as simple as I thought. <div dir="auto">So I limit my proposal to try expressions since I really think they are worth it. </div><div dir="auto">A try expression would follow this syntax:</div><div dir="auto">TARGET VARIABLE = TRY_KEYWORD [RECOURSE_BLOCK] TRY_BODY(with every path leading to a yield or throw) [CATCH_BLOCKS(with also a yield or throw in every path)] </div><div dir="auto">[FINALLY_BLOCK (with no yield but throw possible)];</div><div dir="auto"><br></div><div dir="auto">An example would be:</div><div dir="auto">int fileInt = try (var reader = new BufferedReader(new FileReader("test.txt"))) {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>var line = reader.readLine();</div><div dir="auto"><span style="white-space:pre-wrap"> </span>if (line == null) </div><div dir="auto"><span style="white-space:pre-wrap"> </span>yield -1;</div><div dir="auto"><span style="white-space:pre-wrap"> </span>yield Integer.parseInt(line);</div><div dir="auto">} catch (IOException e) {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>throw new CustomException("Couldn't open file", e);</div><div dir="auto">} catch (NumberFormatException e) {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>yield -1;</div><div dir="auto">} finally {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>if (outsideCondition) </div><div dir="auto"><span style="white-space:pre-wrap"> </span>throw new CustomCancelledExecption();</div><div dir="auto"><span style="white-space:pre-wrap"> </span>System.out.println("file read done");</div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">This example is constructed to feature all possible parts of the syntax and is not necessarily code that makes sense to write. </div><div dir="auto"><br></div><div dir="auto">Great regards </div><div dir="auto">RedIODev </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Mar 4, 2023, 10:55 Red IO <<a href="mailto:redio.development@gmail.com" rel="noreferrer noreferrer" target="_blank">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"><div dir="auto">Currently we have the switch expression as the only expression returning a result.<div dir="auto">Example:</div><div dir="auto">boolean b = switch (1) {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>case 0 -> false;</div><div dir="auto"><span style="white-space:pre-wrap"> </span>case 1 -> {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>yield true;</div><div dir="auto"><span style="white-space:pre-wrap"> </span>} </div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">The idea would be to expand this syntax to different expressions for example the try expression:</div><div dir="auto"><br></div><div dir="auto">int i = try {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>yield Integer.parseInt("Abc");</div><div dir="auto">} catch (NumberFormatException e) {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>yield -1;</div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">Or loops:</div><div dir="auto"><br></div><div dir="auto">String searched = for(String s : args) {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>if (s.startsWith("-"))</div><div dir="auto"><span style="white-space:pre-wrap"> </span>yield s;</div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">The idea is to make all java control flow expressions able to yield a value. </div><div dir="auto"><br></div><div dir="auto">Among many new patterns possible like the examples above it would for example "clean up" the exception catching initialization:</div><div dir="auto"><br></div><div dir="auto">Foo foo = null;</div><div dir="auto"><br></div><div dir="auto">try {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>foo = new Foo();</div><div dir="auto">} catch (SomeException e) {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>//do something or nothing </div><div dir="auto">} finally {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>//maybe change the value again</div><div dir="auto">} </div><div dir="auto"><br></div><div dir="auto">To</div><div dir="auto"><br></div><div dir="auto">var foo = try {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>yield new Foo();</div><div dir="auto">} catch (SomeException e) {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>//throw or yield</div><div dir="auto">} finally {</div><div dir="auto"><span style="white-space:pre-wrap"> </span>//throw or do nothing </div><div dir="auto">};</div><div dir="auto"><br></div><div dir="auto">Another benefit especially in this example is the clearer state of the result. In an yielding expression you have to either yield or throw. Also subsequent manipulation in for example the finally block is not possible making the source of the returned result clear to identify.</div><div dir="auto"><br></div><div dir="auto">Great regards </div><div dir="auto">RedIODev </div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>