Expanding the "expression syntax"
Red IO
redio.development at gmail.com
Sat Mar 4 09:55:30 UTC 2023
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/20230304/8fc06e1b/attachment.htm>
More information about the amber-dev
mailing list