Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I evaluate a lazy val using reflection?

Using the experimental Scala 2.10 reflection, when I try to call FieldMirror.get() on a field that is a lazy val, it returns null. Is there a way to evaluate the lazy val using reflection? The get() function should no longer return null afterwards.

Consider this example:

case class Person(val firstName: String,
                  val lastName: String) {
  lazy val name = firstName + " " + lastName
}

import scala.reflect.runtime.{universe => ru}

val runtimeMirror = ru.runtimeMirror(getClass.getClassLoader)

val fred = Person("Fred", "Smith")

val instanceMirror = runtimeMirror.reflect(fred)

val nameTerm = ru.typeOf[Person].declaration(ru.newTermName("name")).asTerm

val nameLazy = instanceMirror.reflectField(nameTerm)

nameLazy.get
    >>> res8: Any = null

fred.name
    >>> res9: String = Fred Smith

nameLazy.get
    >>> res10: Any = Fred Smith
like image 500
vertexshader Avatar asked May 03 '13 14:05

vertexshader


1 Answers

I discovered the solution. There is a hidden method that gets generated:

case class Person(val firstName: String,
              val lastName: String) {
 lazy val name = firstName + " " + lastName
}

import scala.reflect.runtime.{universe => ru}

val runtimeMirror = ru.runtimeMirror(getClass.getClassLoader)

val fred = Person("Fred", "Smith")

val instanceMirror = runtimeMirror.reflect(fred)

val nameMethod = ru.typeOf[Person].member(ru.newTermName("name")).asMethod
val nameValue = ru.typeOf[Person].member(ru.newTermName("name")).asTerm

val nameLazy = instanceMirror.reflectField(nameValue)
val nameLazyMethod = instanceMirror.reflectMethod(nameMethod)

nameLazy.get
    >>> null

nameLazyMethod()
    >>> res9: String = Fred Smith

nameLazy.get
    >>> res10: String = Fred Smith
like image 138
vertexshader Avatar answered Nov 07 '22 00:11

vertexshader