I am working in Java 19, and using the pattern matching for instanceof that was released in JEP 394 (which released in Java 16). However, I am running into a warning that I am struggling to understand.

public class ExpressionTypeIsASubsetOfPatternType
{

   public record Triple(int a, int b, int c) {}
   
   public static void main(String[] args)
   {
   
      System.out.println("Java Version = " + System.getProperty("java.version"));
   
      final Triple input = new Triple(1, 2, 3);
      
      if (input instanceof Triple t)
      {
      
         System.out.println("Made it here");
      
      }
   
   }

}

And here is the warning that is returned.

$ javac -Xlint:preview --enable-preview --release 19 UnconditionalPatternsPreviewWarning.java
UnconditionalPatternsPreviewWarning.java:15: warning: [preview] unconditional patterns in instanceof are a preview feature and may be removed in a future release.
      if (input instanceof Triple t)
                           ^
1 warning

What does this warning message mean? More specifically, what does an unconditional pattern mean? I tried to search on StackOverflow, but found nothing helpful or useful on this.

I understand well enough that, whatever it is, is a preview feature. And thus, I am trying to do something that has not yet been released. But this looks and sounds like the most basic possible pattern match using the most basic form of pattern-matching --- instanceof. And the JEP that I linked above made it sound like this feature is released.

I guess whatever it is I am doing is an unconditional pattern. But what does that mean?

1

There are 1 answers

0
davidalayachew On BEST ANSWER

So, documentation is frustratingly scarce on this. The best thing I could find (which also happens to be an Oracle resource) was a blog written by Nicolai Parlog -- Pattern matching updates for Java 19’s JEP 427: when and null. Most of the article is unrelated to what I am talking about, but about halfway down, there is a reference to unconditional patterns.

...an unconditional pattern, that is, a pattern that matches all possible instances of the switched variable’s type.

Nicolai is talking about Pattern-matching for Switch, but I am willing to bet that the same rules apply to Pattern-matching for instanceof.

In short, the article is saying that, if the variable you are pattern matching is guaranteed to be fully matched against the pattern you are matching against, then that qualifies as an unconditional pattern.

More specifically, if I make a variable String s, then the compiler knows 100% for certain that s is a String. Therefore, if I try to do something like if (s instanceof String match) {}, then this will be considered to be an unconditional pattern. After all, a String is a String. So, asking if s is a String will always return true.

And it is this concept, unconditional patterns, that is in preview. Pattern-matching for instanceof has been fully released since Java 16. But this unconditional patterns concept is a new functionality that the Java team is considering to add onto pattern-matching for instanceof. Since they are still figuring it out, they are putting it in preview for the time being.