Expression switch - an alternate proposal
forax at univ-mlv.fr
forax at univ-mlv.fr
Fri Apr 13 07:27:12 UTC 2018
----- Mail original -----
> De: "Cay Horstmann" <cay.horstmann at sjsu.edu>
> À: "Brian Goetz" <brian.goetz at oracle.com>, "Remi Forax" <forax at univ-mlv.fr>
> Cc: "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Vendredi 13 Avril 2018 00:57:20
> Objet: Re: Expression switch - an alternate proposal
[...]
>
> What about variable declarations? In statement switch, the following is
> legal:
>
> switch (foo) {
> case 1:
> int x = 1;
> default:
> x = 2;
> System.out.println(x);
> }
>
> and the following is not:
>
> switch (foo) {
> case 1:
> int x = 1;
> System.out.println(x);
> break;
> case 2:
> int x = 2;
> System.out.println(x);
> break;
> }
>
> It seems that, if variable declarations are allowed in an expression
> switch, the rules are going to have to be different :-)
>
> If you shore this up by restricting the statements that can follow a
> case label in an expression switch, haven't you implicitly defined a
> block expression? In that case, why not make it a general construct?
Here disabling fallthrough for the expression switch is an enabler.
Once there is no fallthrough, you can consider that the statements of a case are in an implicit block scope,
i.e
var foo = switch(bar) {
case 0:
int x = 1;
break x;
default:
break 42;
};
to be equivalent to
var foo = switch(bar) {
case 0: { // open a block scope
int x = 1;
break x;
} // end it
default:
break 42;
};
but it can only works for an expression switch and if fallthroughs are disallowed, something we have not yet decided.
>
> Cheers,
>
> Cay
>
>
cheers,
Remi
More information about the amber-dev
mailing list