Created
July 3, 2012 13:01
-
-
Save andy1138/3039589 to your computer and use it in GitHub Desktop.
Scala Dojo - Macro
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
/* | |
* scalac -language:experimental.macros Macros2.scala | |
* scalac -language:experimental.macros Test.scala | |
* scala Test | |
*/ | |
import scala.reflect.makro.Context | |
import language.experimental.macros | |
import scala.reflect.makro.Context | |
import collection.mutable.ListBuffer | |
import collection.mutable.Stack | |
object Macros { | |
var incDebug = false | |
def debug(msg:String) = macro debugImpl | |
def debugImpl[C <: Context](c : C)(msg: c.Expr[String]) = { | |
import c.mirror._ | |
import c.universe._ | |
import Flag._ | |
import definitions._ | |
import language.reflectiveCalls | |
import c.universe._ | |
Literal(Constant(s_msg: String)) = msg | |
c.reify{ if(incDebug) println("DEBUG: Hello World" ) else println("no debug") } | |
} | |
} | |
----- | |
Test.scala | |
import Macros._ | |
object Test extends App { | |
debug("This is debug") | |
println("The End") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment