From 38c7dc3bb8b81bebd7dc6a9e092cec0c10d30a9f Mon Sep 17 00:00:00 2001 From: Martin Egri Date: Tue, 13 Jun 2017 15:09:37 +0200 Subject: Disambiguate old-style/new-style macro compiler plugin builds --- .../new-style-macros-example/macros/Main.scala | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 examples/new-style-macros-example/macros/Main.scala (limited to 'examples/new-style-macros-example/macros/Main.scala') 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 }" + } +} + -- cgit v1.2.3