Exhaustiveness and mutual exclusion

Brian Goetz brian.goetz at oracle.com
Thu Apr 4 17:48:29 UTC 2024


I don't think "exhaustiveness" need imply "independence."  As an example 
from the bytecode API, we have factories for aload_0, aload(0), and 
load(INT, 0), all of which produce the same bytecode.  It is reasonable 
for a "bytecode cursor" to have instance patterns for each of these too, 
and for the sum of all the patterns for all the bytecodes to be 
exhaustive, even though there is clearly overlap.

On 4/4/2024 1:34 PM, Tagir Valeev wrote:
> 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/04bd27c2/attachment.htm>


More information about the amber-spec-observers mailing list