Exhaustiveness and instance patterns

Tagir Valeev amaembo at gmail.com
Thu Apr 4 17:29:54 UTC 2024


Hello!

Suppose we declare several instance patterns and define that they form an
exhaustive set (using syntax from the bikeshed thread):

class X {
  case pattern(String that) p1() {...}
  case pattern(String that) p2() {...}
}

To match, we need an instance of type X. Could it be an arbitrary
expression, or it should be a limited thing (e.g., only a local variable)?
And how the exhaustiveness will be determined? E.g.:

X myX = ...
switch(str) {
  case myX.p1() -> {...}
  case myX.p2() -> {...}
}

Here, we can assume that the set of cases is exhaustive, because p1() and
p2() have the same effectively-final qualifier. But what if it's a method
call?

switch(str) {
  case getX().p1() -> {...}
  case getX().p2() -> {...}
}

The `getX()` method may return different instances of X, and it's not
evident anymore whether this set of patterns is exhaustive. Do we have any
strategy regarding this case? Or exhaustive sets are not allowed for
instance patterns?

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


More information about the amber-spec-observers mailing list