Created
December 7, 2012 16:29
-
-
Save travisbrown/4234441 to your computer and use it in GitHub Desktop.
Simple macro-based wrapper example
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
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
object WrapperExample { | |
def wrap[A](a: A): A = macro wrap_impl[A] | |
def wrap_impl[A: c.WeakTypeTag](c: Context)(a: c.Expr[A]) = { | |
import c.universe._ | |
val wrapped = weakTypeOf[A] | |
val f = Select(reify(Predef).tree, "println") | |
val methods = wrapped.declarations.collect { | |
case m: MethodSymbol if !m.isConstructor => DefDef( | |
Modifiers(Flag.OVERRIDE), | |
m.name, | |
Nil, Nil, | |
TypeTree(), | |
Block( | |
Apply(f, c.literal("Calling: " + m.name.decoded).tree :: Nil), | |
Select(a.tree, m.name) | |
) | |
) | |
}.toList | |
val constructor = DefDef( | |
Modifiers(), nme.CONSTRUCTOR, Nil, Nil :: Nil, TypeTree(), | |
Block( | |
Apply( | |
Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), Nil | |
) :: Nil, | |
c.literalUnit.tree | |
) | |
) | |
val anon = c.fresh | |
c.Expr(Block( | |
ClassDef(Modifiers(Flag.FINAL), newTypeName(anon), Nil, | |
Template( | |
List(Ident(wrapped.typeSymbol.name)), | |
emptyValDef, | |
constructor :: methods | |
) | |
) :: Nil, | |
Apply(Select(New(Ident(newTypeName(anon))), nme.CONSTRUCTOR), Nil) | |
)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A discussion of a subtle error in this code: http://groups.google.com/group/scala-internals/browse_thread/thread/492560d941b315cc