summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRex Kerr <ichoran@gmail.com>2014-02-13 16:28:36 -0800
committerRex Kerr <ichoran@gmail.com>2014-02-18 03:37:00 -0800
commit9be10bc084e8da77a773d6736c6a232ecd40b0c0 (patch)
tree02a3ee555eaa4aff9af89305f0863b26db0d097a
parent8536c3148d5a6283b580a905ca4231e852525d59 (diff)
downloadscala-9be10bc084e8da77a773d6736c6a232ecd40b0c0.tar.gz
scala-9be10bc084e8da77a773d6736c6a232ecd40b0c0.tar.bz2
scala-9be10bc084e8da77a773d6736c6a232ecd40b0c0.zip
SI-8072 rationalize public implicits in scala parallel collections
Pretty much everything seems like it's intended for internal use, so I moved it to a package-private object. Split toParArray out and put it in an implicit class. Added ability to .toParArray from Array and String also. Added test to verify implicits are gone.
-rw-r--r--src/library/scala/collection/parallel/ParIterableLike.scala2
-rw-r--r--src/library/scala/collection/parallel/ParSeqLike.scala1
-rw-r--r--src/library/scala/collection/parallel/package.scala72
-rw-r--r--test/files/neg/t8072.check4
-rw-r--r--test/files/neg/t8072.scala6
5 files changed, 53 insertions, 32 deletions
diff --git a/src/library/scala/collection/parallel/ParIterableLike.scala b/src/library/scala/collection/parallel/ParIterableLike.scala
index 6ab76bce89..445edd23cb 100644
--- a/src/library/scala/collection/parallel/ParIterableLike.scala
+++ b/src/library/scala/collection/parallel/ParIterableLike.scala
@@ -29,6 +29,8 @@ import scala.annotation.unchecked.uncheckedVariance
import scala.annotation.unchecked.uncheckedStable
import scala.language.{ higherKinds, implicitConversions }
+import scala.collection.parallel.ParallelCollectionImplicits._
+
/** A template trait for parallel collections of type `ParIterable[T]`.
*
diff --git a/src/library/scala/collection/parallel/ParSeqLike.scala b/src/library/scala/collection/parallel/ParSeqLike.scala
index ca21f24534..0b6fec364e 100644
--- a/src/library/scala/collection/parallel/ParSeqLike.scala
+++ b/src/library/scala/collection/parallel/ParSeqLike.scala
@@ -16,6 +16,7 @@ import scala.collection.generic.CanBuildFrom
import scala.collection.generic.CanCombineFrom
import scala.collection.generic.VolatileAbort
+import scala.collection.parallel.ParallelCollectionImplicits._
/** A template trait for sequences of type `ParSeq[T]`, representing
* parallel sequences with element type `T`.
diff --git a/src/library/scala/collection/parallel/package.scala b/src/library/scala/collection/parallel/package.scala
index 923e21e5a7..91c54fa8f1 100644
--- a/src/library/scala/collection/parallel/package.scala
+++ b/src/library/scala/collection/parallel/package.scala
@@ -53,43 +53,52 @@ package object parallel {
c
}
- /* implicit conversions */
-
- implicit def factory2ops[From, Elem, To](bf: CanBuildFrom[From, Elem, To]) = new FactoryOps[From, Elem, To] {
- def isParallel = bf.isInstanceOf[Parallel]
- def asParallel = bf.asInstanceOf[CanCombineFrom[From, Elem, To]]
- def ifParallel[R](isbody: CanCombineFrom[From, Elem, To] => R) = new Otherwise[R] {
- def otherwise(notbody: => R) = if (isParallel) isbody(asParallel) else notbody
- }
- }
- implicit def traversable2ops[T](t: scala.collection.GenTraversableOnce[T]) = new TraversableOps[T] {
- def isParallel = t.isInstanceOf[Parallel]
- def isParIterable = t.isInstanceOf[ParIterable[_]]
- def asParIterable = t.asInstanceOf[ParIterable[T]]
- def isParSeq = t.isInstanceOf[ParSeq[_]]
- def asParSeq = t.asInstanceOf[ParSeq[T]]
- def ifParSeq[R](isbody: ParSeq[T] => R) = new Otherwise[R] {
- def otherwise(notbody: => R) = if (isParallel) isbody(asParSeq) else notbody
- }
- def toParArray = if (t.isInstanceOf[ParArray[_]]) t.asInstanceOf[ParArray[T]] else {
- val it = t.toIterator
- val cb = mutable.ParArrayCombiner[T]()
- while (it.hasNext) cb += it.next
- cb.result
- }
- }
- implicit def throwable2ops(self: Throwable) = new ThrowableOps {
- def alongWith(that: Throwable) = (self, that) match {
- case (self: CompositeThrowable, that: CompositeThrowable) => new CompositeThrowable(self.throwables ++ that.throwables)
- case (self: CompositeThrowable, _) => new CompositeThrowable(self.throwables + that)
- case (_, that: CompositeThrowable) => new CompositeThrowable(that.throwables + self)
- case _ => new CompositeThrowable(Set(self, that))
+ /** Adds toParArray method to collection classes. */
+ implicit class CollectionsHaveToParArray[C, T](c: C)(implicit asGto: C => scala.collection.GenTraversableOnce[T]) {
+ def toParArray = {
+ val t = asGto(c)
+ if (t.isInstanceOf[ParArray[_]]) t.asInstanceOf[ParArray[T]]
+ else {
+ val it = t.toIterator
+ val cb = mutable.ParArrayCombiner[T]()
+ while (it.hasNext) cb += it.next
+ cb.result
+ }
}
}
}
package parallel {
+ /** Implicit conversions used in the implementation of parallel collections. */
+ private[collection] object ParallelCollectionImplicits {
+ implicit def factory2ops[From, Elem, To](bf: CanBuildFrom[From, Elem, To]) = new FactoryOps[From, Elem, To] {
+ def isParallel = bf.isInstanceOf[Parallel]
+ def asParallel = bf.asInstanceOf[CanCombineFrom[From, Elem, To]]
+ def ifParallel[R](isbody: CanCombineFrom[From, Elem, To] => R) = new Otherwise[R] {
+ def otherwise(notbody: => R) = if (isParallel) isbody(asParallel) else notbody
+ }
+ }
+ implicit def traversable2ops[T](t: scala.collection.GenTraversableOnce[T]) = new TraversableOps[T] {
+ def isParallel = t.isInstanceOf[Parallel]
+ def isParIterable = t.isInstanceOf[ParIterable[_]]
+ def asParIterable = t.asInstanceOf[ParIterable[T]]
+ def isParSeq = t.isInstanceOf[ParSeq[_]]
+ def asParSeq = t.asInstanceOf[ParSeq[T]]
+ def ifParSeq[R](isbody: ParSeq[T] => R) = new Otherwise[R] {
+ def otherwise(notbody: => R) = if (isParallel) isbody(asParSeq) else notbody
+ }
+ }
+ implicit def throwable2ops(self: Throwable) = new ThrowableOps {
+ def alongWith(that: Throwable) = (self, that) match {
+ case (self: CompositeThrowable, that: CompositeThrowable) => new CompositeThrowable(self.throwables ++ that.throwables)
+ case (self: CompositeThrowable, _) => new CompositeThrowable(self.throwables + that)
+ case (_, that: CompositeThrowable) => new CompositeThrowable(that.throwables + self)
+ case _ => new CompositeThrowable(Set(self, that))
+ }
+ }
+ }
+
trait FactoryOps[From, Elem, To] {
trait Otherwise[R] {
def otherwise(notbody: => R): R
@@ -111,7 +120,6 @@ package parallel {
def isParSeq: Boolean
def asParSeq: ParSeq[T]
def ifParSeq[R](isbody: ParSeq[T] => R): Otherwise[R]
- def toParArray: ParArray[T]
}
@deprecated("This trait will be removed.", "2.11.0")
diff --git a/test/files/neg/t8072.check b/test/files/neg/t8072.check
new file mode 100644
index 0000000000..9267010135
--- /dev/null
+++ b/test/files/neg/t8072.check
@@ -0,0 +1,4 @@
+t8072.scala:4: error: value ifParSeq is not a member of List[Int]
+ val y = x.ifParSeq[Int](throw new Exception).otherwise(0) // Shouldn't compile
+ ^
+one error found
diff --git a/test/files/neg/t8072.scala b/test/files/neg/t8072.scala
new file mode 100644
index 0000000000..2c8213e34a
--- /dev/null
+++ b/test/files/neg/t8072.scala
@@ -0,0 +1,6 @@
+class NoIfParSeq {
+ import collection.parallel._
+ val x = List(1,2)
+ val y = x.ifParSeq[Int](throw new Exception).otherwise(0) // Shouldn't compile
+ val z = x.toParArray
+} \ No newline at end of file