Last active
August 29, 2015 14:05
-
-
Save MasseGuillaume/52cd8a1da6cc960c2263 to your computer and use it in GitHub Desktop.
vars & vals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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