diff options
author | Martin Egri <meg@speedledger.se> | 2017-06-13 15:09:37 +0200 |
---|---|---|
committer | Martin Egri <meg@speedledger.se> | 2017-06-13 15:09:37 +0200 |
commit | 38c7dc3bb8b81bebd7dc6a9e092cec0c10d30a9f (patch) | |
tree | 7a0cdec601addcfe487d299dcad25dc85c3dd7e8 /examples/new-style-macros-example/macros/Main.scala | |
parent | 7d8d01dfb5dc934dc57f08d9e24be38c67270ea3 (diff) | |
download | cbt-38c7dc3bb8b81bebd7dc6a9e092cec0c10d30a9f.tar.gz cbt-38c7dc3bb8b81bebd7dc6a9e092cec0c10d30a9f.tar.bz2 cbt-38c7dc3bb8b81bebd7dc6a9e092cec0c10d30a9f.zip |
Disambiguate old-style/new-style macro compiler plugin builds
Diffstat (limited to 'examples/new-style-macros-example/macros/Main.scala')
-rw-r--r-- | examples/new-style-macros-example/macros/Main.scala | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/examples/new-style-macros-example/macros/Main.scala b/examples/new-style-macros-example/macros/Main.scala new file mode 100644 index 0000000..ecdce34 --- /dev/null +++ b/examples/new-style-macros-example/macros/Main.scala @@ -0,0 +1,25 @@ +package macroparadise_example.macros + +import scala.meta._ +import scala.collection.immutable.Seq + +class Main extends scala.annotation.StaticAnnotation { + inline def apply(defn: Any): Any = meta { + defn match { + case q"object $name { ..$stats }" => + MainMacroImpl.expand(name, stats) + case _ => + abort("@main must annotate an object.") + } + } +} + +// This is an example how we can refactor the macro implementation into a utility +// function which can be used for unit testing, see MainUnitTest. +object MainMacroImpl { + def expand(name: Term.Name, stats: Seq[Stat]): Defn.Object = { + val main = q"def main(args: Array[String]): Unit = { ..$stats }" + q"object $name { $main }" + } +} + |