Skip to content

Instantly share code, notes, and snippets.

@MasseGuillaume
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasseGuillaume/52cd8a1da6cc960c2263 to your computer and use it in GitHub Desktop.
Save MasseGuillaume/52cd8a1da6cc960c2263 to your computer and use it in GitHub Desktop.
vars & vals
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
class V {
val a = 1
var b = 2
}
import scala.reflect.runtime.{universe => ru}
val m = ru.runtimeMirror(getClass.getClassLoader)
val v = new V()
def getTypeTag[T: ru.TypeTag](obj: T) = ru.typeTag[T]
val theType = getTypeTag(v).tpe
theType.members.collect {
case v if v.isTerm => v.asTerm
}.filter{ case t =>
t.isVar || t.isVal
}
// List(variable b, value a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment