Skip to content

Instantly share code, notes, and snippets.

@seraphr
Created November 28, 2016 15:31
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 seraphr/b068dc57ea542a073d00376e93290085 to your computer and use it in GitHub Desktop.
Save seraphr/b068dc57ea542a073d00376e93290085 to your computer and use it in GitHub Desktop.
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
/**
*/
object MethodsMacro {
def apply[T]: Seq[String] = macro genMethods[T]
def genMethods[T: c.WeakTypeTag](c: Context): c.Tree = {
import c.universe._
val tType = implicitly[WeakTypeTag[T]].tpe
val tTypes = tType.members.map(_.name.toString)
q"Seq(..${tTypes})"
}
}
object CompanionMethods {
def apply[T]: Seq[String] = macro genMethods[T]
def genMethods[T: c.WeakTypeTag](c: Context): c.Tree = {
import c.universe._
val tType = implicitly[WeakTypeTag[T]].tpe.dealias
val tCompanion = tType.companion
println(tType)
println(tCompanion)
val tTypes = tCompanion.members.map(_.name.toString)
q"Seq(..${tTypes})"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment