Skip to content

Instantly share code, notes, and snippets.

@proboscis
Created February 15, 2014 06:05
Show Gist options
  • Save proboscis/9015172 to your computer and use it in GitHub Desktop.
Save proboscis/9015172 to your computer and use it in GitHub Desktop.
implicitly retrieves an instance of Class[T] without allocatio
import scala.language.experimental.macros
import scala.reflect.macros.Context
trait ClassMacro{
/**
* implicitly returns a class of specified type!
**/
implicit def getClassMacro[T]:Class[T] = macro ClassMacroImpl.getClassMacroImpl[T]
}
object ClassMacroImpl{
def getClassMacroImpl[T:c.WeakTypeTag](c:Context):c.Expr[Class[T]] ={
import c.universe._
val typ = c.weakTypeOf[T]
val sym = typ.typeSymbol
/*
println(showRaw(typ))
println(showRaw(sym))
println(showRaw(reify{classOf[Int]}))
*/
c.Expr[Class[T]](Literal(Constant(typ)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment