diff options
author | Paul Phillips <paulp@improving.org> | 2010-10-04 05:28:27 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-10-04 05:28:27 +0000 |
commit | a7fd7d6dc257e396cf2cf22a9e0a60c3ce44a992 (patch) | |
tree | 24dab3548d8cef970a748d149ebd566cce808e64 /test/files/run | |
parent | afea859ef64122f71525405625d0006150d87fb0 (diff) | |
download | scala-a7fd7d6dc257e396cf2cf22a9e0a60c3ce44a992.tar.gz scala-a7fd7d6dc257e396cf2cf22a9e0a60c3ce44a992.tar.bz2 scala-a7fd7d6dc257e396cf2cf22a9e0a60c3ce44a992.zip |
Pattern matching on Array types, working for re...
Pattern matching on Array types, working for reals.
def f[T](a: Array[T]) = a match {
case x: Array[Int] => x(0)
case x: Array[Double] => 2
// etc.
}
I'd also like to thank "instantiateTypeVar" for displacing the
mechanical spiders and giant squid beings which used to fill my
nightmares. Now that I know true horror, I welcome the squid.
Closes #2755, review by odersky.
Diffstat (limited to 'test/files/run')
-rw-r--r-- | test/files/run/bug2755.check | 21 | ||||
-rw-r--r-- | test/files/run/bug2755.scala | 58 |
2 files changed, 79 insertions, 0 deletions
diff --git a/test/files/run/bug2755.check b/test/files/run/bug2755.check new file mode 100644 index 0000000000..4905c0052d --- /dev/null +++ b/test/files/run/bug2755.check @@ -0,0 +1,21 @@ +1 +2 +3 +4 +5 +6 +7 +1 +2 +3 +4 +5 +6 +7 +1 +2 +3 +4 +5 +6 +7 diff --git a/test/files/run/bug2755.scala b/test/files/run/bug2755.scala new file mode 100644 index 0000000000..8d10b56734 --- /dev/null +++ b/test/files/run/bug2755.scala @@ -0,0 +1,58 @@ +// Test cases: the only place we can cut and paste without crying +// ourself to sleep. +object Test { + def f1(a: Any) = a match { + case x: Array[Int] => x(0) + case x: Array[Double] => 2 + case x: Array[Float] => x.sum.toInt + case x: Array[String] => x.size + case x: Array[AnyRef] => 5 + case x: Array[_] => 6 + case _ => 7 + } + def f2(a: Array[_]) = a match { + case x: Array[Int] => x(0) + case x: Array[Double] => 2 + case x: Array[Float] => x.sum.toInt + case x: Array[String] => x.size + case x: Array[AnyRef] => 5 + case x: Array[_] => 6 + case _ => 7 + } + def f3[T](a: Array[T]) = a match { + case x: Array[Int] => x(0) + case x: Array[Double] => 2 + case x: Array[Float] => x.sum.toInt + case x: Array[String] => x.size + case x: Array[AnyRef] => 5 + case x: Array[_] => 6 + case _ => 7 + } + + + def main(args: Array[String]): Unit = { + println(f1(Array(1, 2, 3))) + println(f1(Array(1.0, -2.0, 3.0, 1.0))) + println(f1(Array(1.0f, 2.0f, 3.0f, -3.0f))) + println(f1((1 to 4).toArray map (_.toString))) + println(f1(new Array[Any](10))) // should match as Array[AnyRef] + println(f1(Array(1L))) + println(f1(null)) + + println(f2(Array(1, 2, 3))) + println(f2(Array(1.0, -2.0, 3.0, 1.0))) + println(f2(Array(1.0f, 2.0f, 3.0f, -3.0f))) + println(f2((1 to 4).toArray map (_.toString))) + println(f2(new Array[Any](10))) // should match as Array[AnyRef] + println(f2(Array(1L))) + println(f2(null)) + + println(f3(Array(1, 2, 3))) + println(f3(Array(1.0, -2.0, 3.0, 1.0))) + println(f3(Array(1.0f, 2.0f, 3.0f, -3.0f))) + println(f3((1 to 4).toArray map (_.toString))) + println(f3(new Array[Any](10))) // should match as Array[AnyRef] + println(f3(Array(1L))) + println(f3(null)) + } +} |