Primitives in instanceof and patterns

Daniel Avery danielaveryj at gmail.com
Sat Sep 10 04:22:41 UTC 2022


Admittedly my first thought about folding primitive type conversions into
patterns was, "Oh no," but then seeing the deep symmetry drawn with casts
was pretty compelling.

I wonder if I am correct to assume that this symmetry would also inform how
transitive relationships are handled? For instance, I can use a series of
conversions to transform an Integer to a short:

    Integer x = 3;
    int y = x;
    short z = (short) y;

>From symmetry, I'd assume that I would not be able to "destructure an
Integer to a short" directly, and would instead have to use a symmetric
series of patterns:

    x instanceof int y && y instanceof short z

Bonus question... Would it be legal/equivalent to express this as:

    x instanceof Integer y && y instanceof short z

It is not symmetric with casting, but since null is excluded, it seems that
for destructuring purposes Integer and int share the same set of possible
values.

- Daniel Avery
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-observers/attachments/20220909/10a5fb77/attachment-0001.htm>


More information about the amber-spec-observers mailing list