Blankman
Blankman

Reputation: 266950

In scala 2.13.x, is there a way to loop over the values of a Enumeration where Value is a String?

I have an enumeration like this:

object UserType extends Enumeration {
   type UserType = String
   val member = "member"
   val admin = "admin"
}

Is there a way to get all the values of the Enumeration so it returns:

val userTypes: Set[String] = UserType.values.toSet

For some reason UserType.values returns of type UserType.Value or something, not a string.

Is there a way to accomplish this?

Upvotes: 0

Views: 127

Answers (1)

Amerousful
Amerousful

Reputation: 2545

If you want use Enumeration need set Value to your type:

type UserType = Value

And then define variable as:

val admin = Value("admin")

Otherwise, you don't use a Enumeration.


For get list of all values:

UserType.values.toList

Upvotes: 2

Related Questions