Support for primitive types in instanceof and type patterns

Robbe Pincket robbepincket at live.be
Tue Nov 15 21:11:47 UTC 2022


Hi

I did a quick look over this new jep and had some questions.

1. I was wondering if the following is legal:
```java
Number num = ...;

if (num instanceof byte b) {
    ...
}
```

and if yes, is it equivalent to:
```java
if (num instanceof Byte $b) {
    byte b = $b;
    ...
}
```
or:
```java
if (
    num instanceof Long $l && $l.longValue() instanceof byte b ||
    num instanceof Integer $i && $i.intValue() instanceof byte b ||
    num instanceof Short $s && $s.shortValue() instanceof byte b ||
    num instanceof Byte $b && $b.byteValue() instanceof byte b ||
    num instanceof Character $c && $c.charValue() instanceof byte b ||
    num instanceof Float $f && $f.floatValue() instanceof byte b ||
    num instanceof Double $d && $d.doubleValue() instanceof byte b
) {
    ...
}
```

To me it feels like it should be the second to match with the rest of the idea of primitive matching, but that means a rather small expression hides a lot of complexity

2. How will this interact with valhalla

Currently, if I have a `record Box<T>(T item) {}`, then item is always a reference type. However, as I understand the current plans of valhalla, this won't always be true in the future, and item could be an `int` or a `byte`. How would `intanceof Box(byte x)` end up getting compiled? I suppose that is also a question about how pattern matching interacts with universal/specialized generics in the first place, but it seems to me that this jep could influence that so it should probably be taken into consideration.

Kind regards
Robbe Pincket

From: Angelos Bimpoudis<mailto:angelos.bimpoudis at oracle.com>
Sent: dinsdag 15 november 2022 0:21
To: amber-spec-experts<mailto:amber-spec-experts at openjdk.java.net>
Subject: Support for primitive types in instanceof and type patterns

Dear experts,

The draft JEP for adding support for primitive types in instanceof​ and type patterns, that has been previously discussed on this list, is available at:

https://bugs.openjdk.org/browse/JDK-8288476

Suggestions are welcomed!
Angelos

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-observers/attachments/20221115/f42446f5/attachment-0001.htm>


More information about the amber-spec-observers mailing list