do expression
Sergei Egorov
bsideup at gmail.com
Wed Mar 25 15:10:53 UTC 2020
Hi Remi,
Is it a pseudo-code? Not something one can use in Java 14, right?
Best Regards,
Sergei
On Tue, Mar 24, 2020 at 6:46 PM Remi Forax <forax at univ-mlv.fr> wrote:
> Hi Tagir,
> There already a syntax for that in Java,
> we are using it for lambda and switch, the arrow block syntax.
>
> static final String FIELD = -> {
> try {
> yield initializeField();
> }
> catch(CheckedException e) {
> throw new RuntimeException(e);
> }
> };
>
> Rémi
>
> > De: "Tagir Valeev" <amaembo at gmail.com>
> > À: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> > Envoyé: Mardi 24 Mars 2020 03:23:10
> > Objet: do expression
>
> > Hello!
>
> > Now we have a legal way to execute several statements within an
> expression,
> > yielding the result with a yield statement. This could be done via
> `switch`
> > expression.
>
> > Sometimes it's desired to do this without any switch. One may abuse the
> switch
> > expression feature writing `switch(0) { default -> { ... code block
> ending with
> > 'yield' }}`.
>
> > How about creating a special syntax for such kind of expression. It
> could look
> > like `do { ... code block ending with 'yield' }`?
>
> > E.g. consider:
>
> > class X {
> > static final String field;
>
> > // now we are forced to split field declaration and initialization
> > // also initializer could be long and it could be not evident that its
> main
> > purpose
> > // is to initialize the field
> > static {
> > try {
> > field = initializeField();
> > }
> > catch(CheckedException e) {
> > throw new RuntimeException(e);
> > }
> > }
> > }
>
> > Since Java 14 we can write
>
> > class X {
> > // field declaration and initialization in the same place: easier to
> navigate
> > through code
> > // though we are abusing the switch expression
> > static final String field = switch(0) { default -> {
> > try {
> > yield initializeField();
> > }
> > catch(CheckedException e) {
> > throw new RuntimeException(e);
> > }
> > }};
> > }
>
> > It could be like
>
> > class X {
> > // concise syntax. Now we know that the main block purpose
> > // is to initialize the field
> > static final String field = do {
> > try {
> > yield initializeField();
> > }
> > catch(CheckedException e) {
> > throw new RuntimeException(e);
> > }
> > };
> > }
>
> > It's similar to Perl 'do BLOCK' expression
> > [ https://perldoc.perl.org/functions/do.html |
> > https://perldoc.perl.org/functions/do.html ]
>
> > What do you think?
>
> > With best regards,
> > Tagir Valeev
>
More information about the amber-spec-observers
mailing list