<div dir="ltr"><div style="font-family:monospace" class="gmail_default"><br>Hello Amber Dev Team,<br><br>I was working on some code using Pattern-Matching for instanceof, and then I ran into a surprising warning.<br><br>Here is the code.<br><br>public class UnconditionalPatternsPreviewWarning<br>{<br><br>   public record Triple(int a, int b, int c) {}<br>   <br>   public static void main(String[] args)<br>   {<br>   <br>      System.out.println("Java Version = " + System.getProperty("java.version"));<br>   <br>      final Triple input = new Triple(1, 2, 3);<br>      <br>      if (input instanceof Triple t)<br>      {<br>      <br>         System.out.println("Made it here");<br>      <br>      }<br>   <br>   }<br><br>}<br><br>And here is the warning.<br><br>$ javac -Xlint:preview --enable-preview --release 19 UnconditionalPatternsPreviewWarning.java<br>UnconditionalPatternsPreviewWarning.java:15: warning: [preview] unconditional patterns in instanceof are a preview feature and may be removed in a future release.<br>      if (input instanceof Triple t)<br>                           ^<br>1 warning<br><br>I tried searching for what this warning means, but couldn't find it. More specifically, I couldn't understand what Unconditional Patterns meant. With some help from StackOverflow (<a href="https://stackoverflow.com/questions/74978665">https://stackoverflow.com/questions/74978665</a>), I was led to a blog by Nicolai Parlog, which explained the meaning.<br><br>> ...an unconditional pattern, that is, a <br>> pattern that matches all possible<br>> instances of the switched variable’s type.<br><br>I understand the meaning well enough. Basically, it is saying that the compiler knows 100% that a variable is fully and unconditionally matched by the given pattern. Therefore, it is considered an unconditional pattern. As a result, since unconditional patterns are in preview (while instanceof pattern-matching has since been released as General Availability), then I now understand my warning and why it occurred.<br><br>But that leads to a couple questions.<br><br>The fact that you all made this delineation meant that it is something worth denoting. Why? What about it is so significant that you would denote this? In fact, if I turn off the --enable-preview features flag and the Xlint check, that warning turns into an error for the exact same reason -- my pattern fully encompasses all possible values of my variable. So this is clearly something that needed a big fence put around it.<br><br>Is there some (upcoming) functionality involving pattern-matching that depends on this delineation? <br><br>Why make this delineation for the following snippet, but not the one below it?<br><br>if (input instanceof Triple t) {} //error or warning, depending on what you compile with<br><br>if (input instanceof Triple) {} //compiles fine and runs fine<br><br>Is it because of backwards compatibility?<br><br>Thank you all for your time and your help!<br>David Alayachew</div></div>