enums - Retrieving Scala enumeration constants by name -
i retreiving scala enumeration constants name.
dmitriy yefremov propose solution scala 2.10 (@see http://yefremov.net/blog/scala-enum-by-name/)
the code crash
private def factorymethodsymbol(enumtype: type): methodsymbol = { enumtype.member(newtermname("withname")).asmethod // scala.scalareflectionexception: <none> not method }
i update code use scala 2.11. idea ?
you can existing api, don't need workarounds:
def constantbyname[t <: enumeration](enum: t, key: string): option[t#value] = { enum.values.find(_.tostring == key) }
it works because .values
gives list[enum#value]
, can matching.
Comments
Post a Comment