diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-08-20 10:31:23 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-08-20 10:31:23 -0700 |
commit | 38d5b203745e799d819759ccce2f96655085f537 (patch) | |
tree | 5c0e664803a9682bb25c6b48658d0660d9727a02 /test/files/run/name-based-patmat.scala | |
parent | 1df7b32a15bac0c57d4371b6e081f06d9e145aaf (diff) | |
parent | 01f771e687291803da1983bdac97d9288c069bfe (diff) | |
download | scala-38d5b203745e799d819759ccce2f96655085f537.tar.gz scala-38d5b203745e799d819759ccce2f96655085f537.tar.bz2 scala-38d5b203745e799d819759ccce2f96655085f537.zip |
Merge pull request #2848 from paulp/patmat2
Pattern matcher: extractors become name-based.
Diffstat (limited to 'test/files/run/name-based-patmat.scala')
-rw-r--r-- | test/files/run/name-based-patmat.scala | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/test/files/run/name-based-patmat.scala b/test/files/run/name-based-patmat.scala new file mode 100644 index 0000000000..2c429c141f --- /dev/null +++ b/test/files/run/name-based-patmat.scala @@ -0,0 +1,75 @@ +final class MiniSome[T](val get: T) extends AnyVal { def isEmpty = false } + +package p1 { + class Triple(val x: Any) extends AnyRef with Product3[String, String, String] { + private def s = "" + x + override def canEqual(x: Any) = this eq x.asInstanceOf[AnyRef] + def isEmpty = false + def get = this + def _1 = s + def _2 = "2 " + s + "s! A ha ha!" + def _3 = "3 " + s + "s! A ha ha!" + + override def toString = s"Triple(${_1}, ${_2}, ${_3})" + } + + object Triple { + def unapply(x: Any): Triple = new Triple(x) + } +} + +package p2 { + class Triple(val x: Any) { + private def s = "" + x + def isEmpty = false + def get = this + def _1 = s + def _2 = "2 " + s + "s! A ha ha!" + def _3 = "3 " + s + "s! A ha ha!" + override def toString = s"Triple(${_1}, ${_2}, ${_3})" + } + + object Triple { + def unapply(x: Any): Triple = new Triple(x) + } +} + +package p3 { + case class Foo(x: Int, y: Int, zs: Int*) + + object Bar { + def f(x: Foo) = x match { + case Foo(5, 10, 15, 20, _*) => 1 + case Foo(5, 10, 15, _*) => 2 + case Foo(5, 10, _*) => 3 + case Foo(5, 10) => 4 // should warn unreachable + case _ => 5 + } + } +} + +object Test { + + // def f(x: Any) = x match { + // case p1.Foo(x, y, z) => println((x, y, z)) + // case x => println(x) + // } + + def main(args: Array[String]): Unit = { + "catdog" match { + case p1.Triple(x, y, z) => List(x, y, z) foreach println + case x => println("fail: " + x) + } + // TODO + "catdog" match { + case p2.Triple(x, y, z) => List(x, y, z) foreach println + case x => println("fail: " + x) + } + + println(p3.Bar.f(p3.Foo(5, 10, 15, 20, 25))) + println(p3.Bar.f(p3.Foo(5, 10, 15, 20))) + println(p3.Bar.f(p3.Foo(5, 10, 15))) + println(p3.Bar.f(p3.Foo(5, 10))) + // println(p3.Bar.f(p3.Foo(5))) + } +} |