Created
November 20, 2012 18:08
-
-
Save xeno-by/4119716 to your computer and use it in GitHub Desktop.
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
19:07 ~/Projects/Kepler_typemacros/sandbox (topic/typemacros)$ scalac Macros.scala | |
19:07 ~/Projects/Kepler_typemacros/sandbox (topic/typemacros)$ scala -cp . | |
Welcome to Scala version 2.10.1-20121120-175733-b60f5bcf2c (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> class C extends AnyRef with Macros.Foo[Int]("2") | |
defined class C | |
scala> new C().hello | |
res0: String = I've been created from Macros.Foo$macro[Int]("2") | |
scala> 19:10 ~/Projects/Kepler_typemacros/sandbox (topic/typemacros)$ cat Macros.scala | |
import scala.reflect.macros.Context | |
import language.experimental.macros | |
object Macros { | |
def impl[T: c.WeakTypeTag](c: Context)(x: c.Expr[Any]): c.Tree = { | |
import c.universe._ | |
val key = "SomeUniqueName.scala" | |
if (!c.enclosingRun.existsSynthetic(key)) { | |
val msg = "I've been created from " + c.macroApplication | |
val synthetic = reify{ trait SomeUniqueName { def hello = c.literal(msg).splice } }.tree | |
c.enclosingRun.compileSynthetic(key, synthetic) | |
} | |
Ident(newTypeName("SomeUniqueName")) | |
} | |
type Foo(x: Int) = macro impl[String] | |
type Foo[T](x: String) = macro impl[T] | |
}19:10 ~/Projects/Kepler_typemacros/sandbox (topic/typemacros)$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment