Scala 2.10 reflection, how do I extract the field values from a case class -
how can extract field values case class in scala using new reflection model in scala 2.10? example, using below doesn't pull out field methods
def getmethods[t:typetag](t:t) = typeof[t].members.collect { case m:methodsymbol => m }
i plan pump them into
{field <- fields} { currentmirror.reflect(caseclass).reflectfield(field).get }
methodsymbol
has iscaseaccessor
method allows precisely this:
def getmethods[t: typetag] = typeof[t].members.collect { case m: methodsymbol if m.iscaseaccessor => m }.tolist
now can write following:
scala> case class person(name: string, age: int) defined class person scala> getmethods[person] res1: list[reflect.runtime.universe.methodsymbol] = list(value age, value name)
and method symbols want.
Comments
Post a Comment