<div dir="auto">This is syntactically ambiguous, as there are already initializers with similar syntax. Also there are code block lambda bodies, so it would require special parsing rules for lambdas. Using do-expressions is unambiguous in every context.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 5, 2023, 22:34 tom L <<a href="mailto:tom_L64@hotmail.com">tom_L64@hotmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="FR" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="m_7422600091196641520WordSection1">
<p class="MsoNormal">Hello,</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">This sounds like a cool idea, but why use a keyword (do) at the first place ?<br>
I read the discussion, which mostly was about the do keyword, but I feel like there shouldn’t be a keyword at the first place.</p>
<p class="MsoNormal">int foo = {</p>
<p class="MsoNormal"> yield 5 ;</p>
<p class="MsoNormal">}</p>
<p class="MsoNormal">Blocks already exist, and while they do not return anything currently, you could make so if there is a yield, it would mean it can return a value basically.<br>
It’s similar in my opinion to how you can assign the result of a void method, but you can from any other return type, except that here it’s inferred.</p>
<p class="MsoNormal">It can also be seen as a generalization of a switch statement.</p>
<p class="MsoNormal">(Sorry if my vocabulary is off, but I hope you see my point)</p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Great regards</p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" style="border:none;padding:0cm"><b>From : </b><a href="mailto:amaembo@gmail.com" target="_blank" rel="noreferrer">Tagir Valeev</a><br>
<b>To : </b><a href="mailto:redio.development@gmail.com" target="_blank" rel="noreferrer">Red IO</a><br>
<b>Cc : </b><a href="mailto:amber-dev@openjdk.org" target="_blank" rel="noreferrer">amber-dev</a><br>
<b>Object :</b>Re: Expanding the "expression syntax"</p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">Hello!<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Just for the record, three years ago I posted a similar, though more universal proposal, "do expressions":<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><a href="https://mail.openjdk.org/pipermail/amber-spec-experts/2020-March/002046.html" target="_blank" rel="noreferrer">https://mail.openjdk.org/pipermail/amber-spec-experts/2020-March/002046.html</a><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">With best regards,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Tagir Valeev.<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Wed, Apr 5, 2023 at 11:58 AM Red IO <<a href="mailto:redio.development@gmail.com" target="_blank" rel="noreferrer">redio.development@gmail.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<p class="MsoNormal">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. <u></u><u></u></p>
<div>
<p class="MsoNormal">So I limit my proposal to try expressions since I really think they are worth it. <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">A try expression would follow this syntax:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">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)] <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">[FINALLY_BLOCK (with no yield but throw possible)];<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">An example would be:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">int fileInt = try (var reader = new BufferedReader(new FileReader("test.txt"))) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">var line = reader.readLine();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">if (line == null) <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">yield -1;<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">yield Integer.parseInt(line);<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} catch (IOException e) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">throw new CustomException("Couldn't open file", e);<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} catch (NumberFormatException e) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">yield -1;<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} finally {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">if (outsideCondition) <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">throw new CustomCancelledExecption();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">System.out.println("file read done");<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">};<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">This example is constructed to feature all possible parts of the syntax and is not necessarily code that makes sense to write. <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Great regards <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">RedIODev <u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Sat, Mar 4, 2023, 10:55 Red IO <<a href="mailto:redio.development@gmail.com" target="_blank" rel="noreferrer">redio.development@gmail.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<p class="MsoNormal">Currently we have the switch expression as the only expression returning a result.<u></u><u></u></p>
<div>
<p class="MsoNormal">Example:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">boolean b = switch (1) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">case 0 -> false;<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">case 1 -> {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">yield true;<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">};<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">The idea would be to expand this syntax to different expressions for example the try expression:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">int i = try {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">yield Integer.parseInt("Abc");<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} catch (NumberFormatException e) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">yield -1;<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">};<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Or loops:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">String searched = for(String s : args) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">if (s.startsWith("-"))<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">yield s;<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">};<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">The idea is to make all java control flow expressions able to yield a value. <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Among many new patterns possible like the examples above it would for example "clean up" the exception catching initialization:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Foo foo = null;<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">try {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">foo = new Foo();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} catch (SomeException e) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">//do something or nothing <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} finally {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">//maybe change the value again<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">To<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">var foo = try {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">yield new Foo();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} catch (SomeException e) {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">//throw or yield<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">} finally {<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">//throw or do nothing <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">};<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">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.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Great regards <u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</blockquote>
</div>
<p class="MsoNormal" style="margin-left:9.6pt">RedIODev <u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</blockquote></div>