diff options
author | Paul Phillips <paulp@improving.org> | 2013-08-17 09:58:54 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2013-08-17 10:58:14 -0700 |
commit | 1cd7a9e840158dab17a3aafc0ce849605706a561 (patch) | |
tree | 3777a5c8bdfe0edcdcd45aa453d28704242a8702 /test/files/run/name-based-patmat.scala | |
parent | 8f05647ca53da781b420be0723faf1cdbf14b2ff (diff) | |
download | scala-1cd7a9e840158dab17a3aafc0ce849605706a561.tar.gz scala-1cd7a9e840158dab17a3aafc0ce849605706a561.tar.bz2 scala-1cd7a9e840158dab17a3aafc0ce849605706a561.zip |
New tests for name-based pattern matcher.
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))) + } +} |