summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorMiles Sabin <miles@milessabin.com>2012-04-29 14:23:38 +0100
committerMiles Sabin <miles@milessabin.com>2012-05-17 16:19:41 +0100
commit73f7001d963a80950751b4e13b95b05d2526edc8 (patch)
tree4aaacf85d904087d9d9ef49e663eaafc156c0f87 /src/library
parentce896d6531122410659492c07926a0a293b94afa (diff)
downloadscala-73f7001d963a80950751b4e13b95b05d2526edc8.tar.gz
scala-73f7001d963a80950751b4e13b95b05d2526edc8.tar.bz2
scala-73f7001d963a80950751b4e13b95b05d2526edc8.zip
Added infrastructure to enable easy enrichment of GenTraversables.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/collection/GenTraversableLike.scala9
-rw-r--r--src/library/scala/collection/generic/FromRepr.scala56
-rw-r--r--src/library/scala/collection/generic/package.scala6
3 files changed, 71 insertions, 0 deletions
diff --git a/src/library/scala/collection/GenTraversableLike.scala b/src/library/scala/collection/GenTraversableLike.scala
index 0d51230623..eaec7a2a76 100644
--- a/src/library/scala/collection/GenTraversableLike.scala
+++ b/src/library/scala/collection/GenTraversableLike.scala
@@ -411,3 +411,12 @@ trait GenTraversableLike[+A, +Repr] extends Any with GenTraversableOnce[A] with
def stringPrefix: String
}
+
+object GenTraversableLike {
+ /** Manufacture a conversion from collection representation type `Repr` to
+ * its corresponding `GenTraversableLike` given an implicitly available
+ * instance of `FromRepr[Repr]`.
+ * @see [[scala.collection.generic.FromRepr]]
+ */
+ implicit def fromRepr[Repr](implicit fr : FromRepr[Repr]) = fr.hasElem
+}
diff --git a/src/library/scala/collection/generic/FromRepr.scala b/src/library/scala/collection/generic/FromRepr.scala
new file mode 100644
index 0000000000..c08761332c
--- /dev/null
+++ b/src/library/scala/collection/generic/FromRepr.scala
@@ -0,0 +1,56 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2012, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.collection
+package generic
+
+/** Type class witnessing that a collection representation type `Repr` has
+ * elements of type `A` and has a conversion to `GenTraversableLike[A, Repr]`.
+ *
+ * This type enables simple enrichment of `GenTraversable`s with extension
+ * methods which can make full use of the mechanics of the Scala collections
+ * framework in their implementation.
+ *
+ * Example usage,
+ * {{{
+ * import scala.collection.generic.{ CanBuildFrom, FromRepr, HasElem }
+ *
+ * class FilterMapImpl[A, Repr](val r : Repr)(implicit hasElem : HasElem[Repr, A]) {
+ * def filterMap[B, That](f : A => Option[B])
+ * (implicit cbf : CanBuildFrom[Repr, B, That]) : That = r.flatMap(f(_).toSeq)
+ * }
+ *
+ * implicit def filterMap[Repr : FromRepr](r : Repr) = new FilterMapImpl(r)
+ *
+ * val l = List(1, 2, 3, 4, 5)
+ * List(1, 2, 3, 4, 5) filterMap (i => if(i % 2 == 0) Some(i) else None)
+ * // == List(2, 4)
+ * }}}
+ *
+ * @author Miles Sabin
+ * @since 2.10
+ */
+trait FromRepr[Repr] {
+ type A
+ val hasElem: HasElem[Repr, A]
+}
+
+object FromRepr {
+ import language.higherKinds
+
+ implicit val stringFromRepr : FromRepr[String] { type A = Char } = new FromRepr[String] {
+ type A = Char
+ val hasElem = implicitly[HasElem[String, Char]]
+ }
+
+ implicit def genTraversableLikeFromRepr[C[_], A0]
+ (implicit hasElem0: HasElem[C[A0], A0]) : FromRepr[C[A0]] { type A = A0 } = new FromRepr[C[A0]] {
+ type A = A0
+ val hasElem = hasElem0
+ }
+}
diff --git a/src/library/scala/collection/generic/package.scala b/src/library/scala/collection/generic/package.scala
index 32006b4bf0..e0351ebae6 100644
--- a/src/library/scala/collection/generic/package.scala
+++ b/src/library/scala/collection/generic/package.scala
@@ -4,6 +4,12 @@ import generic.CanBuildFrom
package object generic {
type CanBuild[-Elem, +To] = CanBuildFrom[Nothing, Elem, To]
+ /** The type of conversions from a collection representation type
+ * `Repr` to its corresponding GenTraversableLike.
+ * @see [[scala.collection.generic.FromRepr]]
+ */
+ type HasElem[Repr, A] = Repr => GenTraversableLike[A, Repr]
+
@deprecated("use ArrayTagTraversableFactory instead", "2.10.0")
type ClassManifestTraversableFactory[CC[X] <: Traversable[X] with GenericClassManifestTraversableTemplate[X, CC]] = ArrayTagTraversableFactory[CC]