title: Is it possible in Scala 3 to do pattern matching by generic type?
body: <p>I'm trying to migrate my mind from Java background (and a bit of Scala 2) to Scala 3 and to implement a kind of version of <code>PartialFunction.orElse</code> but with union type as an input parameter instead of intersection type:</p>
  <pre class="lang-scala prettyprint-override"><code>trait F[A, B] extends (A =&gt; B) {
    
    def orElse[A1, B1](other: F[A1, B1]): F[A | A1, B | B1] = {
      val self = this
      
      new F[A | A1, B | B1] {
        override def apply(v: A | A1): B | B1 = {
          helper(v)
        }
      
        transparent inline def helper(v: A | A1): Any = {
          inline v match {
            case _: A =&gt; self.apply(v.asInstanceOf[A])
            case _: A1 =&gt; other.apply(v.asInstanceOf[A1])
          }
        }
      }
    }
  }
  </code></pre>
  <p>But I ended up with a fact that both calls go to the first branch of &quot;match&quot;:</p>
  <pre class="lang-scala prettyprint-override"><code> val f1 = new F[Int, String] {
      override def apply(v: Int): String = s&quot;Int =&gt; String : $v&quot;
    }
  
    val f2 = new F[String, Int] {
      override def apply(v: String): Int = v.length
    }
  
    val f = f1.orElse(f2)
  
  println(f(42)) // prints &quot;Int =&gt; String : 42&quot;
  println(f(&quot;hello&quot;)) // fails in runtime because of trying casting String to Int
  </code></pre>
  <p>So, my question is, is it even possible? If yes, what am I missing?</p>