title: How to account for all cases of an enum on the right-hand side of a pattern match
body: <p>Exhaustive pattern matching is great, but it only appears to work on the left-hand side of the case (<code>=&gt;</code>) operator. </p>
  
  <p>I am curious if there is a way that a person can verify that the <em>output</em> of a function (or expression) can be bound to that enumeration. The goal would be to have the compiler tell me when I forget to output an item from the enumeration. </p>
  
  <p>In my example, I make use of the following enumeration containing three items:</p>
  
  <pre><code>object MyEnum { 
      sealed trait t 
      case object E1 extends t
      case object E2 extends t
      case object E3 extends t
  }
  </code></pre>
  
  <p>And here is a pattern-match expression that would produce a compile-time warning (as we already know):</p>
  
  <pre><code>def foo( e : MyEnum.t ) : Boolean =
      e match {
          case MyEnum.E1 =&gt; true
          case MyEnum.E2 =&gt; false
          case MyEnum.E3 =&gt; true   // if we leave this line out, we get a warning
      }
  </code></pre>
  
  <p>Scala would complain if we left <code>MyEnum.E3</code> out of the pattern matching expression, citing a non-exhaustive pattern match. This is profoundly beneficial, but I wonder if the reverse is possible. </p>
  
  <p>Can we account for all cases of <code>MyEnum.t</code> on the right-hand side of <code>=&gt;</code>?</p>
  
  <p>Here is an example that highlights this:</p>
  
  <pre><code>def bar( s : String ) : Option[MyEnum.t] = 
      s match {
          case "a" =&gt; Some(MyEnum.E1)
          case "b" =&gt; Some(MyEnum.E2)
          case "c" =&gt; Some(MyEnum.E3)  // if we leave this out, no warning
          case _ =&gt; None
      }
  </code></pre>
  
  <p>In this example, if we leave out the line with <code>MyEnum.E3</code>, then the compiler just carries on as if nothing is wrong. The assertion I would like to make, is:</p>
  
  <pre><code>forall MyEnum.t (aliased as e) there exists Some(e) | None
  </code></pre>
  
  <p>I understand that this could be easily covered by a run-time test, but I'm curious if there is a way to check this statically.</p>
  
  <p>Thanks.</p>