Knocking off two more vestiges of legacy switch

John Rose john.r.rose at oracle.com
Mon Sep 12 22:58:47 UTC 2022


It’s too harsh to say your example shows the semantics are just wrong.

I think they are right, but possibly incomplete.  The exclusion of case 
200 is the job of dead code detection logic in the language, the same 
kind of logic that also reports an error on `"foo" instanceof List`.

Then there are the old murky rules that allow an integral constant like 
100 to assign to `byte` only because 100 fits in the byte range while 
200 does not.  The duals of those rules will surely speak to the 
restriction of `case 200:` matching a byte.

On 12 Sep 2022, at 15:29, Remi Forax wrote:

>> No new rules; just appeal to type patterns.

> It shows that the semantics you propose for the primitive type pattern 
> is not the right one.
>
> Currently, a code like this does not compile
> byte b = ...
> switch(b) {
> case 200 -> ....
> }
>
> because 200 is not a short which is great because otherwise at runtime 
> it will never be reached.
>
> But if we apply the rules above + your definition of the primitive 
> pattern, the code above will happily compile because it is equivalent 
> to
>
> byte b = ...
> switch(b) {
> case short s when s == 200 -> ....
> }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-observers/attachments/20220912/60b10307/attachment.htm>


More information about the amber-spec-observers mailing list