Disallowing break label (and continue label) inside an expression switch

Gavin Bierman gavin.bierman at oracle.com
Tue Mar 27 14:17:36 UTC 2018


> On 23 Mar 2018, at 20:51, Guy Steele <guy.steele at oracle.com> wrote:
> 
> 
>  String s = switch (e) {
> 	case 0 -> break “foofoo”;
> 	case 1:
> 		if (p == 0) break x;
> 		else { String z = hairy(x); break z+z; }
> 	case 2 -> “barbar”;
>  };
> 
> Now I decide that case 1 has three subcases.  So I change the `if` to a statement `switch`.
> 
>  String s = switch (e) {
> 	case 0 -> break “foofoo”;
> 	case 1:
> 		switch (p) {
> 			case 0: break x;
> 			case 1: break x+x;
> 			default: String z = hairy(x); break z+z;
> 		}
> 	case 2 -> “barbar”;
>  };
> 
> FAIL.

The inner switch is actually an expression switch, so you just need an extra break:

 String s = switch (e) {
	case 0 -> “foofoo”;
	case 1:
		break switch (p) {
			case 0: break x;
			case 1: break x+x;
			default: String z = hairy(x); break z+z;
		};
	case 2 -> “barbar”;
 };


> All I’m doing is demonstrating that a common refactoring pattern “turn the `if` statement into a `switch` statement” has more pitfalls than it used to once we introduce `switch` expressions.

Agreed.

Gavin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20180327/cacae037/attachment.html>


More information about the amber-spec-experts mailing list