Scala convert string to enum with values

Viewed 5077

I started learning scala and I have little problem. I want to convert string value into Enum but occur error No value found for "TEST". What mistake did I?

object Function1 extends Enumeration {
    val TEST = Value("1")
    val TES = Value("2")
    val TE = Value("3")
  }
 println(Function1 withName "TEST")
2 Answers

You should use one of the followings:

println(Function1.TEST)

or

println(Function1.withName("1"))

You are misunderstanding the name of Enumeration, the name in Enumeration actually is the Value's parameter, for your example should be 1

/** Creates a fresh value, part of this enumeration, called name.

@param name A human-readable name for that value.

@return Fresh value called name.

Value(name: String): Value = Value(nextId, name)

and for withName definition:

Return a Value from this Enumeration whose name matches the argument s. The names are determined automatically via reflection.

Related