vertexshader
vertexshader

Reputation: 324

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

Upvotes: 8

Views: 979

Answers (1)

vertexshader
vertexshader

Reputation: 324

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

Upvotes: 7

Related Questions