Exhaustiveness and mutual exclusion

Tagir Valeev amaembo at gmail.com
Thu Apr 4 17:34:27 UTC 2024


Hello!

Another question: if we declare an exhaustive set of patterns, does it
imply that they should be mutually exclusive? This is probably not so
important for compiler, but could be important for IDE functions like
inspections or refactorings. E.g.:

switch(opt) {
 case Optional.of(var x) -> {...}
 case Optional.empty() -> {...}
}
Is it safe to reorder branches in this switch? This is true for Optional,
but can we say this for any custom set of exhaustive patterns?

int x = 0;
if (opt instanceof Optional.of(var x)) {x++;}
if (opt instanceof Optional.empty()) {x++;}

Can a static analyzer assume that only one `if` body will be visited here
(i.e., x is never 2)? Again, an analyzer may hardcode this knowledge for
Optional, but would it be true for any set of exhaustive patterns?

With kind regards,
Tagir Valeev.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-observers/attachments/20240404/c8d33a63/attachment.htm>


More information about the amber-spec-observers mailing list