Question regarding exhaustiveness check of record patterns
Remi Forax
forax at univ-mlv.fr
Wed Nov 2 19:11:51 UTC 2022
> From: "Tesla Ice Zhang" <ice1000kotlin at foxmail.com>
> To: "amber-dev" <amber-dev at openjdk.org>
> Sent: Wednesday, November 2, 2022 7:59:52 PM
> Subject: Question regarding exhaustiveness check of record patterns
> Hi,
> I am confused by a compile error generated by the following code:
> sealed interface I<T> {}
> record A<T>(T t) implements I<T> {}
> public static void main(I<String> args) {
> switch (args) {
> case A<String>(var x) -> System.out.println(x);
> }
> }
> Compiling the code with the following JDK:
> openjdk 19 2022-09-20
> OpenJDK Runtime Environment (build 19+37)
> OpenJDK 64-Bit Server VM (build 19+37, mixed mode, sharing)
> I get:
> .\JavaBug.java:5: error: the switch statement does not cover all possible input
> values
> switch (args) {
> ^
> Note: .\JavaBug.java uses preview features of Java SE 19.
> Note: Recompile with -Xlint:preview for details.
> 1 error
> What cases could be not covered? It looks like a bug to me.
It is a bug, i'm able to reproduce it.
TeslaSwitchTest.java:6: error: the switch statement does not cover all possible input values
switch (args) {
^
Note: TeslaSwitchTest.java uses preview features of Java SE 20.
Note: Recompile with -Xlint:preview for details.
1 error
As a workaround, using a type parameter works
static <T> void main(I<T> args) {
switch (args) {
case A<T>(var x) -> System.out.println(x);
}
}
> Also, I believe that showing the particular uncovered cases in the error
> messages would make very good developer experiences.
yes !
> Best regards,
> Tesla
Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20221102/95590f31/attachment-0001.htm>
More information about the amber-dev
mailing list