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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -