title: Pattern matching on Class<T>?
body: <p>Suppose we have the following hierarchy:</p>
  <pre class="lang-java prettyprint-override"><code>    sealed interface Animal permits Dog, Cat, Bear {}
      record Dog() implements Animal {}
      record Cat() implements Animal {}
      record Bear() implements Animal {}
  </code></pre>
  <p>And we want to create a method which pattern matches over a <code>Class&lt;T extends Animal&gt;</code>.</p>
  <p><strong>Why am I prevented from doing this? Is it because of erasure?</strong></p>
  <p>So if I do the following it won't work:</p>
  <pre class="lang-java prettyprint-override"><code>    static &lt;T extends Animal&gt; String makeNoisePatternMatch(Class&lt;? extends Animal&gt; cls) {
          return switch(cls) {
              case Dog.class c -&gt; &quot;WOOF&quot;;
              case Cat.class c -&gt; &quot;MEOW&quot;;
              case Bear.class c -&gt; &quot;ROAR&quot;;
          };
      }
  
  </code></pre>
  <p>But I can still do this (which works):</p>
  <pre class="lang-java prettyprint-override"><code>    static &lt;T extends Animal&gt; String makeNoise(Class&lt;T&gt; cls) {
          if(cls.equals(Dog.class)) {
              return &quot;WOOF&quot;;
          } else if(cls.equals(Cat.class)) {
              return &quot;MEOW&quot;;
          } else if(cls.equals(Bear.class)) {
              return &quot;ROAR&quot;;
          } else {
              throw new IllegalStateException(&quot;Unknown state&quot;);
          }
      }
  </code></pre>
  <p>IntelliJ actually hints that you should change the above to use switch and then produces a the switch expression above (which does not work).</p>
  <p>Again - my question is: <strong>Why am I prevented from doing this? Is it because of erasure?</strong></p>