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/tailrec.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/tailrec.scala')
-rw-r--r-- | test/files/neg/tailrec.scala | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/test/files/neg/tailrec.scala b/test/files/neg/tailrec.scala new file mode 100644 index 0000000000..4c45672f93 --- /dev/null +++ b/test/files/neg/tailrec.scala @@ -0,0 +1,53 @@ +import scala.annotation.tailrec + +// putting @tailrec through the paces +object Main { + @tailrec + def facfail(n: Int): Int = + if (n == 0) 1 + else n * facfail(n - 1) + + @tailrec + def facsucc(n: Int, acc: Int): Int = + if (n == 0) acc + else facsucc(n - 1, n * acc) + + @tailrec def loopy1(x: Int): Int = loopy1(x - 1) + + def ding { + object dong { + @tailrec def loopy2(x: Int): Int = loopy2(x) + } + () + } + + def inner(q: Int) = { + @tailrec + def loopy3(x: Int): Int = loopy3(x + 1) + + loopy3(q) + } +} + +class Bob { + // these should work + @tailrec private def succ1(x: Int): Int = succ1(x) + @tailrec final def succ2(x: Int): Int = succ2(x) + @tailrec final def succ3[T](in: List[T], acc: List[T]): List[T] = in match { + case Nil => Nil + case x :: xs => succ3(xs, x :: acc) + } + + // not private, not final + @tailrec def fail1(x: Int): Int = fail1(x) + + // a typical between-chair-and-keyboard error + @tailrec def fail2[T](xs: List[T]): List[T] = xs match { + case Nil => Nil + case x :: xs => x :: fail2(xs) + } + + object innerBob { + @tailrec def succ4(x: Int): Int = succ4(x) + } +} |