使用Scala 2.10反射,如何列出Enumeration的值? [英] Using Scala 2.10 reflection how can I list the values of Enumeration?
本文介绍了使用Scala 2.10反射,如何列出Enumeration的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有以下枚举
object ResponseType extends Enumeration {
val Listing, Album = Value
}
如何获取其值列表?
推荐答案
如果要对此进行详细介绍,则需要检查您的符号是否具有 Value
作为超类型:
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
您的当前方法将在此处包含 value someNonValueThing
。
Your current approach would include value someNonValueThing
here.
这篇关于使用Scala 2.10反射,如何列出Enumeration的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文