5

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 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer, is it possible to create enum with function like in java?
You can use trait with case object to simulate Java's enum
4

You should use one of the followings:

println(Function1.TEST)

or

println(Function1.withName("1"))

1 Comment

Thanks for answer. I thought its work a little difference. Is it possible to create function to each enum like in java?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.