Having a following enumeration
object ResponseType extends Enumeration {
val Listing, Album = Value
}
How do I get a list of its vals?
If you want to be thorough about this, you need to check that your symbols have Value
as a supertype:
def valueSymbols[E <: Enumeration: TypeTag] = {
val valueType = typeOf[E#Value]
typeOf[E].members.filter(sym => !sym.isMethod &&
sym.typeSignature.baseType(valueType.typeSymbol) =:= valueType
)
}
Now even if you have the following (which is perfectly legal):
object ResponseType extends Enumeration {
val Listing, Album = Value
val someNonValueThing = "You don't want this in your list of value symbols!"
}
You still get the correct answer:
scala> valueSymbols[ResponseType.type] foreach println
value Album
value Listing
Your current approach would include value someNonValueThing
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With