Created
February 15, 2014 06:05
-
-
Save proboscis/9015172 to your computer and use it in GitHub Desktop.
implicitly retrieves an instance of Class[T] without allocatio
This file contains hidden or 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
| 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