summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-02-19 19:59:40 +0000
committerPaul Phillips <paulp@improving.org>2011-02-19 19:59:40 +0000
commit68d13416b50acb1779a3befe817fb2a7eabe9f14 (patch)
tree7c4ba0d3b702b6bc05edebd91a43a681a13d2110 /src
parente91c0e25f1095e77b8dcc8342893462d072a35e9 (diff)
downloadscala-68d13416b50acb1779a3befe817fb2a7eabe9f14.tar.gz
scala-68d13416b50acb1779a3befe817fb2a7eabe9f14.tar.bz2
scala-68d13416b50acb1779a3befe817fb2a7eabe9f14.zip
Rediscovering that transpose sometimes throws a...
Rediscovering that transpose sometimes throws an exception on irregular lists and sometimes returns an irregular result (depending on whether the first collection is the longest or not) indicated that this needs a better resolution than the status quo. Determination: as long as we're throwing an exception on any invalid input, we should throw an exception on all invalid input, so that's what it does now. I postpone any attempt to offer a variation accepting a "hole value". Closes #3597, review by community.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/collection/generic/GenericTraversableTemplate.scala12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/library/scala/collection/generic/GenericTraversableTemplate.scala b/src/library/scala/collection/generic/GenericTraversableTemplate.scala
index 6a1bff6cec..d6f01718ff 100644
--- a/src/library/scala/collection/generic/GenericTraversableTemplate.scala
+++ b/src/library/scala/collection/generic/GenericTraversableTemplate.scala
@@ -12,6 +12,7 @@ package scala.collection
package generic
import mutable.Builder
+import annotation.migration
import annotation.unchecked.uncheckedVariance
/** A template class for companion objects of ``regular`` collection classes
@@ -136,18 +137,27 @@ trait GenericTraversableTemplate[+A, +CC[X] <: Traversable[X]] extends HasNewBui
* element type of this $coll is a `Traversable`.
* @return a two-dimensional $coll of ${coll}s which has as ''n''th row
* the ''n''th column of this $coll.
+ * @throws `IllegalArgumentException` if all collections in this $coll
+ * are not of the same size.
*/
+ @migration(2, 9, "As of 2.9, transpose throws an exception if collections are not uniformly sized.")
def transpose[B](implicit asTraversable: A => /*<:<!!!*/ TraversableOnce[B]): CC[CC[B] @uncheckedVariance] = {
if (isEmpty)
return genericBuilder[CC[B]].result
- val bs: IndexedSeq[Builder[B, CC[B]]] = IndexedSeq.fill(asTraversable(head).size)(genericBuilder[B])
+ def fail = throw new IllegalArgumentException("transpose requires all collections have the same size")
+
+ val headSize = asTraversable(head).size
+ val bs: IndexedSeq[Builder[B, CC[B]]] = IndexedSeq.fill(headSize)(genericBuilder[B])
for (xs <- this) {
var i = 0
for (x <- asTraversable(xs)) {
+ if (i >= headSize) fail
bs(i) += x
i += 1
}
+ if (i != headSize)
+ fail
}
val bb = genericBuilder[CC[B]]
for (b <- bs) bb += b.result