Skip to content

Instantly share code, notes, and snippets.

@number23
Forked from Centaur/fromScalaConsole.scala
Last active March 18, 2017 18:44
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 number23/9ca7d28951b4aec97259fe4e816b8eb2 to your computer and use it in GitHub Desktop.
Save number23/9ca7d28951b4aec97259fe4e816b8eb2 to your computer and use it in GitHub Desktop.
get case class properties using reflection
import scala.reflect.runtime.{universe => ru}
import ru._
def getProperties[T: TypeTag]: Iterable[String] = {
val tpe = ru.typeTag[T].tpe
tpe.decls.collect {
case m: MethodSymbol if m.isCaseAccessor => m.name.toString
}
}
case class User(name: String, age: Int) {
val other: Long = 0
}
assert(getProperties[User] == Iterable("name", "age"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment