When you try to get an object field by its string name, the value is returned, and not by the scala rule. AT:
import scala.language.reflectiveCalls
import scala.language.implicitConversions
case class Intity(flag: Boolean, id: Int, name: String)
val inty = Intity(false, 123, "blue")
implicit def reflect(r: AnyRef) = new {
def get(n:String) = {
val c = r.getClass.getDeclaredField(n)
c.setAccessible(true); c}
def getVal(n: String) = get(n).get(r)
def getType (n:String) = get(n).getType
}
then using this
inty.getType("flag") // res0: Class[_] = boolean --not Boolean
inty.getVal("id") // res1: Object = 123 --Object not Int
Any efficient way to accomplish the above implementation?
ALMEK source
share