aboutsummaryrefslogtreecommitdiff
path: root/examples/new-style-macros-example/macros/Main.scala
diff options
context:
space:
mode:
authorChristopher Vogt <oss.nsp@cvogt.org>2017-06-14 12:21:40 -0400
committerChristopher Vogt <oss.nsp@cvogt.org>2017-06-14 12:21:40 -0400
commite3262d4e4f8b83cac6f1b2a0558af3e30073ec2a (patch)
tree293f6094560094ff5580ba62e4a2d07e02e86a7d /examples/new-style-macros-example/macros/Main.scala
parent66c3d62ebb08eb0054ade9b98dc7a477976117b7 (diff)
parent3df15ef7c9c5a48516c06cf7b598acf298d8b8c0 (diff)
downloadcbt-e3262d4e4f8b83cac6f1b2a0558af3e30073ec2a.tar.gz
cbt-e3262d4e4f8b83cac6f1b2a0558af3e30073ec2a.tar.bz2
cbt-e3262d4e4f8b83cac6f1b2a0558af3e30073ec2a.zip
Merge remote-tracking branch 'origin/master' into kind-projector-plugin
Diffstat (limited to 'examples/new-style-macros-example/macros/Main.scala')
-rw-r--r--examples/new-style-macros-example/macros/Main.scala25
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 }"
+ }
+}
+