title: Declaring an empty pattern matching function
body: <p>How do I declare an empty/case-less pattern matching function to satisfy a type definition? I'm happy with the function just throwing a runtime exception if it's ever called.</p>
  <p>I'm working through the <a href="https://docs.scala-lang.org/tutorials/scala-for-java-programmers.html" rel="nofollow noreferrer">Scala tutorial for Java programmers</a>, in which I have a working function that performs variable substitution on a mathematic expression presented as a tree given a <code>String =&gt; Int</code> map. I want to call the same code path even when no variables should exist (in this case after taking a derivative of the expression), but I can't find a concise way to satisfy the type requirements. Here's the full code I have that works but feels wrong:</p>
  <pre class="lang-scala prettyprint-override"><code>abstract class Tree
  
  case class Sum(l: Tree, r: Tree) extends Tree
  case class Var(n: String) extends Tree
  case class Const(v: Int) extends Tree
  
  object CalculatorPatternsPrime {
    def eval(tree: Tree, env: String =&gt; Int): Int = tree match {
      case Sum(l, r) =&gt; eval(l, env) + eval(r, env)
      case Var(n) =&gt; env(n)
      case Const(v) =&gt; v
    }
  
    def eval(tree: Tree): Int = eval(tree, { case &quot;ignore&quot; =&gt; -1 })
  
    def derive(tree: Tree, v: String): Tree = tree match {
      case Sum(l, r) =&gt; Sum(derive(l, v), derive(r, v))
      case Var(n) if (n == v) =&gt; Const(1)
      case _ =&gt; Const(0)
    }
  
    def main(args: Array[String]): Unit = {
      val env: String =&gt; Int = { case &quot;x&quot; =&gt; 5 case &quot;y&quot; =&gt; 7 }
      val tree = Sum(
        Sum(Const(7), Var(&quot;y&quot;)),
        Sum(Var(&quot;x&quot;), Var(&quot;x&quot;))
      )
      println(eval(tree, env))
      println(derive(tree, &quot;x&quot;))
      println(eval(derive(tree, &quot;x&quot;)))
    }
  }
  </code></pre>
  <p>As you can see I have a dummy <code>{ case &quot;ignore&quot; =&gt; -1 }</code> to make the type system happy, and the code works fine but I feel like there must be a better way to do this. Here are two alternatives I've considered:</p>
  <ol>
  <li>Just writing out a full method body to <code>eval(tree: Tree)</code> instead of trying to call <code>eval(tree: Tree, env: String =&gt; Int)</code> but this duplicates the code handling the <code>Sum</code> and <code>Const</code> cases.</li>
  <li>Making <code>env</code> an optional/union type and letting it throw an NPE.</li>
  </ol>
  <p>What is the idiomatic approach here?</p>