Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6608889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:39:27+00:00 2026-05-25T19:39:27+00:00

Is there any way to automatically parse a case object from a string, in

  • 0

Is there any way to automatically parse a case object from a string, in Scala? Using some built in / automatically generated Scala function?

For example, I have these case objects: (please note that there’s a sealed parent class)

abstract sealed class FlagReason

case object Spam extends FlagReason
case object Illegal extends FlagReason
case object CopyrightViolation extends FlagReason
case object Other extends FlagReason

and I’m wondering if there’s some automatically generated function that works like:

FlagReason.fromString(value: String): FlagReason

where FlagReason("Spam") would return the Spam case object.

If there were, then I need not write my own — which I’ve done:

object FlagReason {
  def fromString(value: String): FlagReason = value match {
    case "Spam" => Spam
    case "Illegal" => Illegal
    case "CopyrightViolation" => CopyrightViolation
    case "Other" => Other
  }
}

Background: 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.

Related info: This is actually possible with Java enums, see e.g. this StackOverflow question: Lookup enum by string value

((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))

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:39:27+00:00Added an answer on May 25, 2026 at 7:39 pm

    No, no such method is automatically generated. You will have to write your own fromString method. Note that you can write it more compactly as follows:

    object FlagReason {
      def fromString(value: String): Option[FlagReason] = {
        Vector(Spam, Illegal, CopyRightViolation, Other).find(_.toString == value)
      }
    }
    

    Alternatively you may consider using scala.Enumeration which does provide this facility.

    object FlagReason extends Enumeration {
      val Spam, Illegal, CopyRightViolation, Other = Value
    }
    

    Then you can obtain the particular enum value with FlagReason withName "<name>", or safely as an Option with Try(FlagReason withName "<name>").toOption.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to have PHP automatically call a function, before a script
is there any way to automatically create hierachical menus from taxonomy vocabulary in Drupal
Is there any way to automatically wrap comments at the 80-column boundary as you
Is there any way to automatically rename accessor/mutator when a variable they get/set gets
Is there any way to automatically launch an application on USB attach or CD
Is there any way to automatically create a trigger on creation of new table
Is there any easy way to automatically deploy a web service / java web
Is there any way to configure IIS or Web app to automatically open a
Is there a way to specify which namespace includes should be automatically added any
Is there any way to check whether a file is locked without using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.