I am using Scala 2.11.0-M5.
I need to find the fields (members that are vars or vals) of a Scala class/type and for each field I need to find the class/type.
So far I have been able to get the field members but I can't figure out how to get the member's type once I have the member.
scala> class Account {
var name: String = null;
var accountNumber: String = null;
} | | |
defined class Account
scala> import reflect.runtime.universe._
import reflect.runtime.universe._
scala> for (m <- typeOf[Account].members.filter(m => !m.isMethod)) {
| println(m)
| // ??? how do I get the member's type ????
| }
variable accountNumber
variable name
You can use typeSignature
:
scala> typeOf[Account].members.filter(!_.isMethod).foreach(
| sym => println(sym + " is a " + sym.typeSignature)
| )
variable accountNumber is a String
variable name is a String
In this context this method will return a reflect.runtime.universe.Type
.
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