sample usage of separate macro configuration

This commit is contained in:
Mark Harrah 2012-09-18 14:01:19 -04:00
parent b8ef434ec1
commit ad92037d34
4 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,26 @@
// Adds a "macro" configuration for macro dependencies.
// By default, this includes the dependencies of the normal sources.
// Drop the `extend(Compile)` to include no dependencies (not even scala-library) by default.
ivyConfigurations += config("macro").hide.extend(Compile)
// add the compiler as a dependency for src/macro/
libraryDependencies <+=
scalaVersion("org.scala-lang" % "scala-compiler" % _ % "macro")
// adds standard compile, console, package tasks for src/macro/
inConfig(config("macro"))(Defaults.configSettings)
// puts the compiled macro on the classpath for the main sources
unmanagedClasspath in Compile <++=
fullClasspath in config("macro")
// includes sources in src/macro/ in the main source package
mappings in (Compile, packageSrc) <++=
mappings in (config("macro"), packageSrc)
// Includes classes compiled from src/macro/ in the main binary
// This can be omitted if the classes in src/macro/ aren't used at runtime
mappings in (Compile, packageBin) <++=
mappings in (config("macro"), packageBin)
scalaVersion := "2.10.0-M7"

View File

@ -0,0 +1,16 @@
import scala.reflect.macros.Context
import language.experimental.macros
object Def
{
def desugar(a: Any): String = macro desugarImpl
def desugarImpl(c: Context)(a: c.Expr[Any]) = {
import c.universe._
val s = show(a.tree)
c.Expr(
Literal(Constant(s))
)
}
}

View File

@ -0,0 +1,8 @@
object Use
{
def main(args: Array[String])
{
val str = Def.desugar(3 + 5)
assert(str == args(0), s"Expected '${args(0)}', got '$str'")
}
}

View File

@ -0,0 +1,2 @@
> run 8
-> run 9