Skip to content

Instantly share code, notes, and snippets.

@gourlaysama
Created August 12, 2014 20:28
Show Gist options
  • Save gourlaysama/474ecb97262380f56fe8 to your computer and use it in GitHub Desktop.
Save gourlaysama/474ecb97262380f56fe8 to your computer and use it in GitHub Desktop.
Using -Xmacro-settings from SBT
scalaVersion in ThisBuild := "2.11.2"
lazy val macros = project settings(
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value,
scalacOptions += "-language:experimental.macros")
lazy val main = project dependsOn(macros) settings(
scalacOptions += "-Xmacro-settings:foo=bar,bippy")
$ tree
.
|-- build.sbt
|-- macros
| `-- macros.scala
|-- main
| `-- test.scala
`-- project
`-- build.properties
3 directories, 4 files
$ sbt 'main/run'
[info] Loading global plugins from /home/gourlaysama/.sbt/0.13/plugins
[info] Loading project definition from /tmp/toto/project
[info] Updating {file:/tmp/toto/project/}toto-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to toto (in build file:/tmp/toto/)
[info] Updating {file:/tmp/toto/}macros...
[info] Resolving jline#jline;2.12 ...
[info] Done updating.
[info] Updating {file:/tmp/toto/}main...
[info] Resolving jline#jline;2.12 ...
[info] Done updating.
[info] Compiling 1 Scala source to /tmp/toto/macros/target/scala-2.11/classes...
[warn] there was one deprecation warning; re-run with -deprecation for details
[warn] one warning found
[info] Compiling 1 Scala source to /tmp/toto/main/target/scala-2.11/classes...
[info] Running Test
Hello World!, Macro Property: bar
[success] Total time: 9 s, completed Aug 12, 2014 10:27:37 PM
import scala.language.experimental.macros
import scala.reflect.macros.Context
object Macros {
def impl(c: Context) = {
import c.universe._
val s = c.settings.find(_.startsWith("foo=")).map(_.substring(4)).orNull
val prop = c.Expr[String](Literal(Constant(s)))
reify {
println("Hello World!, Macro Property: "+prop.splice)
}
}
def hello: Unit = macro impl
}
object Test extends App {
Macros.hello
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment