Skip to content

Instantly share code, notes, and snippets.

@andy1138
Created July 3, 2012 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy1138/3039589 to your computer and use it in GitHub Desktop.
Save andy1138/3039589 to your computer and use it in GitHub Desktop.
Scala Dojo - Macro
/*
* 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