score:33
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
.
score:4
As missingfaktor points out, FlagReason withName "<name>"
should do what you need. But if <name>
is not a valid name, it will throw an exception. So, a slightly safer way to handle this when you are not sure if the name is valid is to use Option[FlagReason]
:
scala> def parse(name: String) = FlagReason.values.find(_.toString == name)
parse: (name: String)Option[FlagReason.Value]
scala> parse("Spam")
res0: Option[FlagReason.Value] = Some(Spam)
scala> parse("NonExisting")
res1: Option[FlagReason.Value] = None
Source: stackoverflow.com
Related Query
- Built in parsing of a string to a Scala case object?
- Scala parsing string to case object
- Read case class object from string in Scala (something like Haskell's "read" typeclass)
- Parsing String in scala to custom case objects
- scala "factory" design pattern for creating case object from string
- Load a String to Case Object in Scala
- Parsing simple json string using scala built in functions
- Scala - case match partial string
- How do I "get" a Scala case object from Java?
- Getting object instance by string name in scala
- Converting JSON string to a JSON object in Scala
- Serializing a scala object into a JSon String using lift-json
- Can a Scala case object be used in a match case
- Deserialization of case object in Scala with JSON4S
- scala case object pollution
- In Scala is there a way to reference the Companion Object from within an instance of a Case Class?
- Easily parse String of Key=Value pairs to Scala case class
- Scala dependency injection when using case class/companion object pattern
- Parsing scala 3 code from a String into Scala 3 AST at runtime
- Scala Jackson object mapper setting null instead of default values in case class
- Ignore case for a string in scala
- Getting all enum-y case object values in Scala
- Knowing if a Scala object is an instance of Case Class
- Implementing a string class that does case insensitive comparisions in Scala
- Parsing a Json String in Scala using Play framework
- Scala syntax - pass string to object
- How to decorate an immutable object graph from scala case classes
- An object extends its companion case class in Scala
- Return a string from a Future onComplete case with Scala and Spray.io
- Parsing JSON and iterating through the object in Scala
More Query from same tag
- What is actuall class for structural type scala syntax definition?
- How can an action convey that it has no body using a type?
- How to add contextmenu and undo/redo functionality to a scala swing textarea
- Recursive blocks in Scala Play Framework templates
- Injecting special cases to Akka Serialization module
- Type mismatch when utilising a case class in a package object
- replace "London\\/India\\/chaina\\/" to "London_India_chaina_"
- What is the rationale behind having companion objects in Scala?
- scala Iterator#foreach performance issue
- Can anybody please recommend me a good library to apply machine learning in java/scala?
- Calling Scala Method from Java - Set<Object> or Set<Long>?
- Difference between Scala 2 implicits and Scala 3 given/using
- How to permanently disable javadsl namespace when coding with Akka Streams
- Scala scope saying that object.propery doesn't exist but left panel in eclipse shows it
- How to make a function working with different collection type in Scala 2.13?
- Difference between 1 :: 2 :: Nil vs. (1 :: (2 :: Nil))?
- Scala spark UDF function that takes input and puts it in an Array
- Thread-Pool with multiple limits
- Serializing Multipart Form requests for testing on Play 2.1
- How to override bang operator(!) for different type of inputs in scala
- How to match classes of "boolean" types and "Boolean" types?
- Purpose of `render` in json4s
- Converting matrix to array row by row in Breeze
- Do IDEs compile your Java or Scala source code to offer static analysis?
- scala actor blocking current thread
- spark unit testing with dataframe : Collect return empty array
- Comparing floating point numbers with tolerance fails assertion
- Slick update optional field
- Difference between type constructors and parametrized type bounds in Scala
- Unable to verify subset of arguments passed to mocked method in specs2