diff options
author | Paul Phillips <paulp@improving.org> | 2009-03-16 17:46:58 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-03-16 17:46:58 +0000 |
commit | 67c3c68da5de6284c5e37ef4b5f907d0079f0490 (patch) | |
tree | 0db5a40f781f2f9edb21fd4c20ce6608c8a5c147 /test/files/neg/switch.scala | |
parent | 00d196adeeb3aa88f2afedcb5d7eb51f0fc19896 (diff) | |
download | scala-67c3c68da5de6284c5e37ef4b5f907d0079f0490.tar.gz scala-67c3c68da5de6284c5e37ef4b5f907d0079f0490.tar.bz2 scala-67c3c68da5de6284c5e37ef4b5f907d0079f0490.zip |
The birth of the @switch and @tailrec annotations.
They are located in package scala.annotation. Also in this patch:
* numerous test cases for both annotations
* addition of @tailrec and @switch in a few strategic locations
* fixes for critical section NewScanners methods which were not being
compiled into switches, immediately proving the value of @switch
* tail recursive implementations for Iterator.{ dropWhile, drop}
and List.dropWhile tagged with @tailrec, closing bug #1376
Diffstat (limited to 'test/files/neg/switch.scala')
-rw-r--r-- | test/files/neg/switch.scala | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/test/files/neg/switch.scala b/test/files/neg/switch.scala new file mode 100644 index 0000000000..a5a6f9e789 --- /dev/null +++ b/test/files/neg/switch.scala @@ -0,0 +1,66 @@ +import scala.annotation.switch + +// this is testing not so much how things ought to be but how they are; +// the test is supposed to start failing if the behavior changes at all. +object Other { + val C1 = 'P' // fails: not final + final val C2 = 'Q' // succeeds: singleton type Char('Q') inferred + final val C3: Char = 'R' // fails: type Char specified + final val C4 = '\u000A' // succeeds like C2 but more unicodey +} + +object Main { + def succ1(c: Char) = (c: @switch) match { + case 'A' | 'B' | 'C' => true + case 'd' => true + case 'f' | 'g' => true + case _ => false + } + + def succ2(c: Char) = (c: @switch) match { + case 'A' | 'B' | 'C' => true + case Other.C2 => true + case Other.C4 => true + case _ => false + } + + // has a guard + def fail1(c: Char) = (c: @switch) match { + case 'A' | 'B' | 'C' => true + case x if x == 'A' => true + case _ => false + } + + // throwing in @unchecked on the next two to make sure + // multiple annotations are processed correctly + + // thinks a val in an object is constant... so naive + def fail2(c: Char) = (c: @switch @unchecked) match { + case 'A' => true + case Other.C1 => true + case _ => false + } + + // more naivete + def fail3(c: Char) = (c: @unchecked @switch) match { + case 'A' => true + case Other.C3 => true + case _ => false + } + + // guard case done correctly + def succ3(c: Char) = (c: @switch) match { + case 'A' | 'B' | 'C' => true + case x => x == 'A' + } + + // some ints just to mix it up a bit + def succ4(x: Int, y: Int) = ((x+y): @switch) match { + case 1 => 5 + case 2 => 10 + case 3 => 20 + case 4 => 50 + case 5|6|7|8 => 100 + case _ => -1 + } +}
\ No newline at end of file |