title: Scala pattern match multiple types
body: <p>I have a code for extracting <code>Int</code> from <code>JValue</code> that should look exactly the same for multiple <code>JValue</code> subclasses, so I'm trying to avoid repeating myself. However, as it is (see below), scala thinks that <code>j</code> is a generic <code>JValue</code>, and that <code>j.values</code> returns a value of type <code>Values</code>, which, of course, does not have <code>isValidInt</code> and <code>toInt</code> methods.</p>
  
  <pre><code>jvalue \ name match {
    case j @ (JInt | JDecimal | JDouble) =&gt; {
      val num = j.values
      if (num.isValidInt) num.toInt.success else reportError(name + " is not a valid int")
    }
  </code></pre>
  
  <p>The question is what is the proper way to avoid repetition here? I'm not that crazy about converting a <code>JValue</code> into string, because this code works with json just parsed from a string into an AST. I started thinking about writing wrappers for the three types I need matched and implicit converters from these types into wrappers, and then making a superclass just for these wrappers for use as a pattern, but I am not sure how to pull it off.</p>
  
  <p>And yes, I realize there are multiple similar questions here (such as <a href="https://stackoverflow.com/questions/15655817/scala-multiple-type-pattern-matching">this</a> and <a href="https://stackoverflow.com/questions/1837754/match-multiple-cases-classes-in-scala">this</a>), but each of them only contains part of the solution at most. </p>