default branch placement in switch
Brian Goetz
brian.goetz at oracle.com
Fri Nov 3 21:25:18 UTC 2017
Yeah, this has to change. In existing switches, there are no case
labels other than default, so order is irrelevant. But now that
patterns have overlapping match-sets, default should be considered to
dominate other cases, so it should go last.
Compatibility-wise, we have two choices for how to get there; carve out
a permanent exception for switches where all cases are type-restating
constant patterns, or plan to eventually get to a place where default
always comes last, even for "int" switches. If we want to get to the
latter, we should start warning on this construct now.
On 11/3/2017 5:10 PM, Tagir Valeev wrote:
> Hello!
>
> Currently the default branch can be placed in any place inside the
> switch operator, e.g. like this:
>
> switch(i) {
> case 1: System.out.println("one");break;
> default: System.out.println("other");break;
> case 2: System.out.println("two");break;
> }
>
> In this case behavior does not change on the order of case blocks.
> However in pattern matching the order of cases usually matters: if
> some pattern matches, this means that the subsequent patterns will not
> be checked. Does this mean that with pattern matching the default
> branch makes all the subsequent case blocks unreachable? Or default
> can still be located anywhere and is checked only after any other
> pattern?
>
> With best regards,
> Tagir Valeev
More information about the amber-spec-experts
mailing list