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

Ron Pressler ron.pressler at oracle.com
Fri Jan 27 08:59:36 UTC 2023



> On 26 Jan 2023, at 22:01, Stephen Colebourne <scolebourne at joda.org> wrote:
> 
>  In this context, `42` is *not* an `int` at all - it is a literal. There is no conversion here,
> `42` is typed as a `byte` because of the assignment. `42` is never, at
> any stage, an `int`.


See JLS §3.10.1 and §5.2.


> There is simply no comparison here. Dog and Animal are subtypes of
> Pet, a concept that has been baked into the language since day one,
> and is fully understood by all. By contrast, there is absolutely no
> subtyping relationship between int, byte and short


Whether or not there’s any comparison here is debatable, but until, like, 
five minutes ago switch couldn’t switch on types, and now that switch *must* 
switch on type relationships is an eternal cornerstone of Java?

Also, you may want to look at JLS §4.10.1.


> saying that the language should now examine not only the type but also
> the value of the instance in order to determine flow control.


I believe switch has examined values to determine control flow for quite some time now.


> The root cause of the issue here is trying to treat Object hierarchy
> conversion and conversion between different primitive types as being
> somehow equivalent.


Not equivalent. Rather, the JEP generalises patterns into a concept that encompasses 
both. It reinterprets what patterns are about: "Despite having been restricted to
reference types, instanceof is in principle about asking whether an upcoming cast of a 
value to a type would succeed without loss of information or error.”

Given that patterns, as a new general concept, are all about reinterpreting old concepts 
and extrapolating from them, pointing out that other extrapolations are possible is not 
exactly calling out the emperor’s new clothes.

— Ron

P.S.

The tone of your email was a jot hysterical, which did not aid comprehension and was implacably annoying.
Maybe tone it down next time




More information about the amber-dev mailing list