Created
July 30, 2012 16:07
-
-
Save Blaisorblade/3208059 to your computer and use it in GitHub Desktop.
Examples of macros: inside reify, only .splice, not .value can be called on Expr
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.reflect.makro.Context | |
import language.experimental.macros | |
def macroId[A](c: Context)(v: c.Expr[A]): c.Expr[A] = c.reify(v.splice) | |
def id2[A](v: A) = macro macroId[A] | |
id2(1) |
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
//Show that with Expr.splice works | |
$ ~/opt/scala-2.10.0-M5/bin/scala -Xlog-free-terms | |
Welcome to Scala version 2.10.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_33). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> import scala.reflect.makro.Context | |
import scala.reflect.makro.Context | |
scala> import language.experimental.macros | |
import language.experimental.macros | |
scala> def macroId[A](c: Context)(v: c.Expr[A]): c.Expr[A] = c.reify(v.splice) | |
macroId: [A](c: scala.reflect.makro.Context)(v: c.Expr[A])c.Expr[A] | |
scala> def id2[A](v: A) = macro macroId[A] | |
id2: [A](v: A)A | |
scala> id2(1) | |
res0: Int = 1 |
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.reflect.makro.Context | |
import language.experimental.macros | |
def macroId[A](c: Context)(v: c.Expr[A]): c.Expr[A] = c.reify(v.value) | |
def id2[A](v: A) = macro macroId[A] | |
id2(1) |
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
//Show problem with Expr.value | |
$ ~/opt/scala-2.10.0-M5/bin/scala -Xlog-free-terms | |
Welcome to Scala version 2.10.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_33). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> import scala.reflect.makro.Context | |
import scala.reflect.makro.Context | |
scala> import language.experimental.macros | |
import language.experimental.macros | |
scala> def macroId[A](c: Context)(v: c.Expr[A]): c.Expr[A] = c.reify(v.value) | |
<console>:9: free term: Ident(newTermName("c")) defined by macroId in <console>:9:23 | |
def macroId[A](c: Context)(v: c.Expr[A]): c.Expr[A] = c.reify(v.value) | |
^ | |
<console>:9: free term: Ident(newTermName("v")) defined by macroId in <console>:9:35 | |
def macroId[A](c: Context)(v: c.Expr[A]): c.Expr[A] = c.reify(v.value) | |
^ | |
macroId: [A](c: scala.reflect.makro.Context)(v: c.Expr[A])c.Expr[A] | |
scala> def id2[A](v: A) = macro macroId[A] | |
id2: [A](v: A)A | |
scala> id2(1) | |
<console>:12: error: Macro expansion contains free term variable v defined by macroId in <console>:9:35. Have you forgotten to use splice when splicing this variable into a reifee? If you have troubles tracking free term variables, consider using -Xlog-free-terms | |
id2(1) | |
^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment