Draft JEP on Primitive types in patterns, instanceof, and switch

Angelos Bimpoudis angelos.bimpoudis at oracle.com
Tue Feb 14 22:22:06 UTC 2023

Hello all,

We've redone the presentation to try to make the motivation & connections more clear. The main points are the following: we lift restrictions around primitive type patterns in nested positions, in top-level positions, around constant case labels and instanceof. The semantics for instanceof​ are naturally derived from pattern matching. Now instanceof​ can safeguard all permitted casts according to Chapter 5.


Let us know of your thoughts.
From: Brian Goetz <brian.goetz at oracle.com>
Sent: 26 January 2023 17:04
To: Guy Steele <guy.steele at oracle.com>
Cc: forax at univ-mlv.fr <forax at univ-mlv.fr>; Angelos Bimpoudis <angelos.bimpoudis at oracle.com>; amber-spec-experts <amber-spec-experts at openjdk.org>
Subject: Re: Draft JEP on Primitive types in patterns, instanceof, and switch

Yes, pardon my quick typing.

More specifically, a case constant is not a _constant pattern_ (at least, not yet), it is just a constant case label.  Constants are typed, and they carray both a type match and a value match.  When you say

    case 0

this is a constant for an int, and doesn't match a floating point zero (if the match target is `float`, it won't even compile.)  Similarly, `case 0.0` is a constant for a float, and is not applicable to a match target of int.  (We do have the usual tolerance for matching int constants to shorter int types, as before.)

We may generalize these to constant patterns in the future, but right now, these are constant case labels and their semantics are derived from that of existing case labels.

On 1/26/2023 10:25 AM, Guy Steele wrote:

On Jan 26, 2023, at 9:36 AM, Brian Goetz <brian.goetz at oracle.com<mailto:brian.goetz at oracle.com>> wrote:


For comparing a variable to a constant, representational equality is the obvious interpretation; `case nnn` means "is it the number nnn".  This allows you to say `case NaN` and `case -0` and get the right answer (all of these relations agree on what to do about 1.0).

Careful: I think you had better say “case -0.0”; otherwise the constant expression “-0” will be reduced to “0” and only then converted to floating-point representation, producing +0.0.

Yes, floating-point is VERY fiddly.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20230214/a6abab23/attachment.htm>

More information about the amber-spec-experts mailing list