Expanding the "expression syntax"

Tagir Valeev amaembo at gmail.com
Thu Apr 6 06:29:06 UTC 2023


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.

On Wed, Apr 5, 2023, 22:34 tom L <tom_L64 at hotmail.com> wrote:

> Hello,
>
>
>
> This sounds like a cool idea, but why use a keyword (do) at the first
> place ?
> 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.
>
> int foo = {
>
>                 yield 5 ;
>
> }
>
> 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.
> 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.
>
> It can also be seen as a generalization of a switch statement.
>
> (Sorry if my vocabulary is off, but I hope you see my point)
>
>
>
> Great regards
>
>
>
> *From : *Tagir Valeev <amaembo at gmail.com>
> *To : *Red IO <redio.development at gmail.com>
> *Cc : *amber-dev <amber-dev at openjdk.org>
> *Object :*Re: Expanding the "expression syntax"
>
>
>
> Hello!
>
>
>
> Just for the record, three years ago I posted a similar, though more
> universal proposal, "do expressions":
>
>
> https://mail.openjdk.org/pipermail/amber-spec-experts/2020-March/002046.html
>
>
>
> With best regards,
>
> Tagir Valeev.
>
>
>
> On Wed, Apr 5, 2023 at 11:58 AM Red IO <redio.development at gmail.com>
> wrote:
>
> 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.
>
> So I limit my proposal to try expressions since I really think they are
> worth it.
>
> A try expression would follow this syntax:
>
> 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)]
>
> [FINALLY_BLOCK (with no yield but throw possible)];
>
>
>
> An example would be:
>
> int fileInt = try (var reader = new BufferedReader(new
> FileReader("test.txt"))) {
>
> var line = reader.readLine();
>
> if (line == null)
>
> yield -1;
>
> yield Integer.parseInt(line);
>
> } catch (IOException e) {
>
> throw new CustomException("Couldn't open file", e);
>
> } catch (NumberFormatException e) {
>
> yield -1;
>
> } finally {
>
> if (outsideCondition)
>
> throw new CustomCancelledExecption();
>
> System.out.println("file read done");
>
> };
>
>
>
> This example is constructed to feature all possible parts of the syntax
> and is not necessarily code that makes sense to write.
>
>
>
> Great regards
>
> RedIODev
>
>
>
> On Sat, Mar 4, 2023, 10:55 Red IO <redio.development at gmail.com> wrote:
>
> Currently we have the switch expression as the only expression returning a
> result.
>
> Example:
>
> boolean b = switch (1) {
>
> case 0 -> false;
>
> case 1 -> {
>
> yield true;
>
> }
>
> };
>
>
>
> The idea would be to expand this syntax to different expressions for
> example the try expression:
>
>
>
> int i = try {
>
> yield Integer.parseInt("Abc");
>
> } catch (NumberFormatException e) {
>
> yield -1;
>
> };
>
>
>
> Or loops:
>
>
>
> String searched = for(String s : args) {
>
> if (s.startsWith("-"))
>
> yield s;
>
> };
>
>
>
> The idea is to make all java control flow expressions able to yield a
> value.
>
>
>
> Among many new patterns possible like the examples above it would for
> example "clean up" the exception catching initialization:
>
>
>
> Foo foo = null;
>
>
>
> try {
>
> foo = new Foo();
>
> } catch (SomeException e) {
>
> //do something or nothing
>
> } finally {
>
> //maybe change the value again
>
> }
>
>
>
> To
>
>
>
> var foo = try {
>
> yield new Foo();
>
> } catch (SomeException e) {
>
> //throw or yield
>
> } finally {
>
> //throw or do nothing
>
> };
>
>
>
> 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.
>
>
>
> Great regards
>
> RedIODev
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230406/e2689a3e/attachment.htm>


More information about the amber-dev mailing list