summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2013-01-07 07:42:38 +0300
committerEugene Burmako <xeno.by@gmail.com>2013-01-09 08:10:46 +0100
commit66acf364ae3003dd1abb4eeb8759afc6e12aa1a1 (patch)
tree853fc3cf39bd34ec32887843fb98f05bad8f1fb7 /test
parentd17e3fc29cd48a3d4cbbbfc1fc9eb021d787d4d7 (diff)
downloadscala-66acf364ae3003dd1abb4eeb8759afc6e12aa1a1.tar.gz
scala-66acf364ae3003dd1abb4eeb8759afc6e12aa1a1.tar.bz2
scala-66acf364ae3003dd1abb4eeb8759afc6e12aa1a1.zip
SI-5903 extractor macros do work
Apparently it is already possible to use macros to customize pattern matching as described in the comments to the aforementioned JIRA issue. What's even better - with the incoming addition of c.introduceTopLevel it becomes possible to generate arbitrarily complex unappliers, even with heterogeneous types of arguments varying from expansion to expansion
Diffstat (limited to 'test')
-rw-r--r--test/files/run/macro-expand-unapply-a.check2
-rw-r--r--test/files/run/macro-expand-unapply-a.flags1
-rw-r--r--test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala15
-rw-r--r--test/files/run/macro-expand-unapply-a/Test_2.scala6
-rw-r--r--test/files/run/macro-expand-unapply-b.check2
-rw-r--r--test/files/run/macro-expand-unapply-b.flags1
-rw-r--r--test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala37
-rw-r--r--test/files/run/macro-expand-unapply-b/Test_2.scala8
8 files changed, 72 insertions, 0 deletions
diff --git a/test/files/run/macro-expand-unapply-a.check b/test/files/run/macro-expand-unapply-a.check
new file mode 100644
index 0000000000..7c2976e51e
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-a.check
@@ -0,0 +1,2 @@
+(1,2)
+(1,2,3)
diff --git a/test/files/run/macro-expand-unapply-a.flags b/test/files/run/macro-expand-unapply-a.flags
new file mode 100644
index 0000000000..cd66464f2f
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-a.flags
@@ -0,0 +1 @@
+-language:experimental.macros \ No newline at end of file
diff --git a/test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala b/test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala
new file mode 100644
index 0000000000..61d6345f16
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala
@@ -0,0 +1,15 @@
+import scala.reflect.macros.Context
+
+object Helper {
+ def unapplySeq[T](x: List[T]): Option[Seq[T]] = List.unapplySeq[T](x)
+}
+
+object Macros {
+ def impl[T: c.WeakTypeTag](c: Context)(x: c.Expr[List[T]]) = {
+ c.universe.reify(Helper.unapplySeq(x.splice))
+ }
+
+ object UnapplyMacro {
+ def unapplySeq[T](x: List[T]): Option[Seq[T]] = macro impl[T]
+ }
+} \ No newline at end of file
diff --git a/test/files/run/macro-expand-unapply-a/Test_2.scala b/test/files/run/macro-expand-unapply-a/Test_2.scala
new file mode 100644
index 0000000000..6169d86b19
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-a/Test_2.scala
@@ -0,0 +1,6 @@
+import Macros._
+
+object Test extends App {
+ List(1, 2) match { case UnapplyMacro(x, y) => println((x, y)) }
+ List(1, 2, 3) match { case UnapplyMacro(x, y, z) => println((x, y, z)) }
+} \ No newline at end of file
diff --git a/test/files/run/macro-expand-unapply-b.check b/test/files/run/macro-expand-unapply-b.check
new file mode 100644
index 0000000000..5272f0d00a
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-b.check
@@ -0,0 +1,2 @@
+(1,List(2))
+List(1)
diff --git a/test/files/run/macro-expand-unapply-b.flags b/test/files/run/macro-expand-unapply-b.flags
new file mode 100644
index 0000000000..cd66464f2f
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-b.flags
@@ -0,0 +1 @@
+-language:experimental.macros \ No newline at end of file
diff --git a/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala b/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala
new file mode 100644
index 0000000000..d0300bdf7e
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala
@@ -0,0 +1,37 @@
+import language.experimental.macros
+import scala.reflect.macros.Context
+
+object Macros {
+ implicit class ContextExtensions(c: StringContext) {
+ object q {
+ def unapply(x: Any): Option[Any] = macro impl
+ }
+ }
+
+ def impl(c: Context)(x: c.Expr[Any]): c.Expr[Option[Any]] = {
+ import c.universe._
+ import Flag._
+
+ // parts here will be string literals - static parts of the string interpolation
+ // e.g. for q"$x, $y" parts will be Literal(Constant("")), Literal(Constant(", ")) and Literal(Constant(""))
+ val Apply(Select(Select(Apply(_, List(Apply(_, parts))), _), _), _) = c.macroApplication
+ val nresults = parts.length - 1
+
+ def results() =
+ ((1 to (nresults - 1)).toList map (i => Literal(Constant(i)))) :+ // (n - 1) results of type Int
+ Apply(Ident(TermName("List")), List(Literal(Constant(nresults)))) // and also one result of a different type
+ def extractorBody() =
+ if (nresults == 0) Literal(Constant(true))
+ else if (nresults == 1) Apply(Ident(TermName("Some")), results())
+ else Apply(Ident(TermName("Some")), List(Apply(Ident(TermName("Tuple" + nresults)), results())))
+
+ val name = TermName(java.util.UUID.randomUUID().toString.replace("-", ""))
+ val mdef = ModuleDef(NoMods, name, Template(List(Select(Ident(TermName("scala")), TypeName("AnyRef"))), emptyValDef, List(
+ DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(),
+ Block(List(pendingSuperCall), Literal(Constant(())))),
+ DefDef(Modifiers(), TermName("unapply"), List(), List(List(ValDef(Modifiers(PARAM), TermName("x"), Ident(TypeName("Any")), EmptyTree))), TypeTree(),
+ extractorBody()))))
+ c.introduceTopLevel(nme.EMPTY_PACKAGE_NAME.toString, mdef)
+ c.Expr[Option[Any]](Apply(Select(Ident(name), TermName("unapply")), List(x.tree)))
+ }
+} \ No newline at end of file
diff --git a/test/files/run/macro-expand-unapply-b/Test_2.scala b/test/files/run/macro-expand-unapply-b/Test_2.scala
new file mode 100644
index 0000000000..5352160dfe
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-b/Test_2.scala
@@ -0,0 +1,8 @@
+object Test extends App {
+ import Macros._
+ def whatever() = null
+ val q"$x1, $y1" = whatever()
+ println(x1, y1)
+ val q"$x2" = whatever()
+ println(x2)
+}