title: Built in parsing of a string to a Scala case object?
body: <p>Is there any way to automatically parse a case object from a string, in Scala? Using some built in / automatically generated Scala function?</p>
  
  <p>For example, I have these case objects:  (<em>please note</em> that there's a sealed parent class)</p>
  
  <pre><code>abstract sealed class FlagReason
  
  case object Spam extends FlagReason
  case object Illegal extends FlagReason
  case object CopyrightViolation extends FlagReason
  case object Other extends FlagReason
  </code></pre>
  
  <p>and I'm wondering if there's some automatically generated function that works like:   </p>
  
  <p><code>FlagReason.fromString(value: String): FlagReason</code></p>
  
  <p>where <code>FlagReason("Spam")</code> would return the <code>Spam</code> case object.</p>
  
  <p>If there were, then I need not write my own -- which I've done:</p>
  
  <pre><code>object FlagReason {
    def fromString(value: String): FlagReason = value match {
      case "Spam" =&gt; Spam
      case "Illegal" =&gt; Illegal
      case "CopyrightViolation" =&gt; CopyrightViolation
      case "Other" =&gt; Other
    }
  }
  </code></pre>
  
  <p><em>Background</em>: I'm converting my case objects to strings that I use as radio button values in a html form. I'm converting the selected value back to a case object, when I handle the submitted form.</p>
  
  <p><em>Related info</em>: This is actually possible with Java enums, see e.g. this StackOverflow question: <a href="https://stackoverflow.com/questions/604424/java-enum-converting-string-to-enum">Lookup enum by string value</a></p>
  
  <p>((I don't think I'm looking for Scala's Parser Combinators. I suppose that were I to use them I'd still need to define the parsing rules myself, rather than having built in "automatic" string to case object conversion))</p>