[External] : Re: Draft JEP on Primitive types in patterns, instanceof, and switch
Ron Pressler
ron.pressler at oracle.com
Fri Jan 27 12:29:30 UTC 2023
Because, at least the way I read it — and perhaps the motivation needs to be clarified — the point is not so much how to just solve the problem of primitive ranges, but how to best understand patterns. Patterns are a language feature; widening and narrowing conversions for both references and primitives — with or without explicit casts — are also a language feature.
The interesting insight in the proposal is about how the patterns language feature interacts with the narrowing conversion language feature. It points out that patterns makes narrowing reference conversion (JLS §5.1.6), aka “downcasts”, safe for reference types, and so it’s a reasonable generalisation to have patterns also make narrowing primitive conversions (JLS §5.1.3) safe as well. If patterns are a “narrowing conversion safeifier” language feature, they should work with all of the language’s narrowing conversions.
— Ron
> On 27 Jan 2023, at 11:21, Red IO <redio.development at gmail.com> wrote:
>
> I don't see why we need a language change here. Why not add safecast methods and is in bounds methods in the primitive wrapper classes. Like:
> Byte.isInBounds(56L)
> And
> byte b = Byte.castOrThrow(56L);
> Or
> Optional<Byte> b = Byte.cast(56L);
> Of cause the latter one would be quite inefficient in the current (pre Valhalla) world.
>
> I don't see many cases where someone might switch on a primitive and have many cases like in the examples (float to int, long, byte..)
>
> Great regards
> RedIODev
More information about the amber-dev
mailing list