<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
Just a gentle reminder — this is straying pretty far outside the charter of amber-dev.
<div class=""><br class="">
</div>
<div class="">Amber-dev is primarily for collaboration on the _implementation_ of Project Amber. This includes things like bug reports and experience reports, “how does it work” queries, etc. We do interpret the charter pretty broadly, and so when discussion
briefly steps outside the bounds, that’s usually not a problem. </div>
<div class=""><br class="">
</div>
<div class="">But, amber-dev is definitely *not* the “I have a new idea for Java” discussion list. With 10M developers, and each of them having two or three favorite feature ideas, such discussion could easily overwhelm the actual work that is going on.
So probably best to mostly limit to features that are actually under development within Project Amber. </div>
<div class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class="">-Brian</div>
<div class=""><br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On Apr 5, 2023, at 10:51 AM, Red IO <<a href="mailto:redio.development@gmail.com" class="">redio.development@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="auto" class="">Yes I know this python syntax.
<div dir="auto" class="">To make it clear saying that I'm not a fan of python would be an understatement.</div>
<div dir="auto" class="">But even the python creator labeled the loop else as his biggest mistake. </div>
<div dir="auto" class="">And adding "the biggest mistake" of my least favorite language to java is definitely not in my interest.</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">When you look in another direction like rust which already features the "everything is an expression" mindset you can see that for and while loops where purposefully left out because of this exact dilemma. Rust features a special endless
loop expression which can be an expression when static analysis can confirm that "yield" will be hit eventually or loop forever.</div>
<div dir="auto" class="">But I don't see java adding this loop type and treating a "while(true)" which is the practical equivalent specially would be weird.</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">Great regards </div>
<div dir="auto" class="">RedIODev </div>
</div>
<br class="">
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Wed, Apr 5, 2023, 19:27 Adowrath <<a href="mailto:adowrath@protonmail.ch" class="">adowrath@protonmail.ch</a>> wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="">
<p class="">Actually, regarding the `else` block/branch for a loop, there's almost a precedence for this in Python: for and while loops can have an `else` block that executes after the loop has finished, iff no break statement (or, of course, return) was hit
inside of the loop, only that this doesn't elevate the loop to an expression. Using the same logic for yielding from an expression loop could work equally - the downside is that even in Python it's considered quite uncommon/unusual syntax.<br class="">
</p>
<p class="">Regards,<br class="">
Adowrath<br class="">
</p>
<div class="">Am 2023-04-05 um 19:17 schrieb Red IO:<br class="">
</div>
<blockquote type="cite" class="">
<div dir="auto" class="">I actually tried this exactly with annotation processor "hacks" I then realized that a loop might execute 0 times or never conditionally hit a yield statements. This results in the expression sometimes not yielding a result which wouldn't
allow an assignment to a final variable and confusing errors like "variable might not be initialized" the only solution would be to add an else block to a loop that os executed when no yield was hit in the loop body (which would be ugly and confusing in my
opinion)
<div dir="auto" class="">That's why I stepped down from generalizing yield to all control flow elements to specially allow yield in try. </div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">Great regards </div>
<div dir="auto" class="">RedIODev </div>
</div>
<br class="">
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Wed, Apr 5, 2023, 16:27 Holo The Sage Wolf <<a href="mailto:holo3146@gmail.com" target="_blank" rel="noreferrer" class="">holo3146@gmail.com</a>> wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="auto" class="">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" class=""><br class="">
</div>
<div dir="auto" class="">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 class="">
<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" rel="noreferrer noreferrer noreferrer" target="_blank" class="">amaembo@gmail.com</a>> wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr" class="">Hello!
<div class=""><br class="">
</div>
<div class="">Just for the record, three years ago I posted a similar, though more universal proposal, "do expressions":</div>
<div class=""><a href="https://mail.openjdk.org/pipermail/amber-spec-experts/2020-March/002046.html" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank" class="">https://mail.openjdk.org/pipermail/amber-spec-experts/2020-March/002046.html</a><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">With best regards,</div>
<div class="">Tagir Valeev.</div>
</div>
<br class="">
<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 noreferrer noreferrer" target="_blank" class="">redio.development@gmail.com</a>> wrote:<br class="">
</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" class="">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" class="">So I limit my proposal to try expressions since I really think they are worth it. </div>
<div dir="auto" class="">A try expression would follow this syntax:</div>
<div dir="auto" class="">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" class="">[FINALLY_BLOCK (with no yield but throw possible)];</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">An example would be:</div>
<div dir="auto" class="">int fileInt = try (var reader = new BufferedReader(new FileReader("test.txt"))) {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>var line = reader.readLine();</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>if (line == null) </div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>yield -1;</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>yield Integer.parseInt(line);</div>
<div dir="auto" class="">} catch (IOException e) {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>throw new CustomException("Couldn't open file", e);</div>
<div dir="auto" class="">} catch (NumberFormatException e) {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>yield -1;</div>
<div dir="auto" class="">} finally {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>if (outsideCondition) </div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>throw new CustomCancelledExecption();</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>System.out.println("file read done");</div>
<div dir="auto" class="">};</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">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" class=""><br class="">
</div>
<div dir="auto" class="">Great regards </div>
<div dir="auto" class="">RedIODev </div>
</div>
<br class="">
<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 noreferrer noreferrer" target="_blank" class="">redio.development@gmail.com</a>> wrote:<br class="">
</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" class="">Currently we have the switch expression as the only expression returning a result.
<div dir="auto" class="">Example:</div>
<div dir="auto" class="">boolean b = switch (1) {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>case 0 -> false;</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>case 1 -> {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>yield true;</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>} </div>
<div dir="auto" class="">};</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">The idea would be to expand this syntax to different expressions for example the try expression:</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">int i = try {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>yield Integer.parseInt("Abc");</div>
<div dir="auto" class="">} catch (NumberFormatException e) {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>yield -1;</div>
<div dir="auto" class="">};</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">Or loops:</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">String searched = for(String s : args) {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>if (s.startsWith("-"))</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>yield s;</div>
<div dir="auto" class="">};</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">The idea is to make all java control flow expressions able to yield a value. </div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">Among many new patterns possible like the examples above it would for example "clean up" the exception catching initialization:</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">Foo foo = null;</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">try {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>foo = new Foo();</div>
<div dir="auto" class="">} catch (SomeException e) {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>//do something or nothing </div>
<div dir="auto" class="">} finally {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>//maybe change the value again</div>
<div dir="auto" class="">} </div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">To</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">var foo = try {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>yield new Foo();</div>
<div dir="auto" class="">} catch (SomeException e) {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>//throw or yield</div>
<div dir="auto" class="">} finally {</div>
<div dir="auto" class=""><span style="white-space:pre-wrap" class=""></span>//throw or do nothing </div>
<div dir="auto" class="">};</div>
<div dir="auto" class=""><br class="">
</div>
<div dir="auto" class="">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" class=""><br class="">
</div>
<div dir="auto" class="">Great regards </div>
<div dir="auto" class="">RedIODev </div>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>