Skip to content

Commit

Permalink
Merge pull request #200 from szeiger/wip/fix-buildfrom
Browse files Browse the repository at this point in the history
Upgrade BuildFrom to the latest definition from 2.13
  • Loading branch information
szeiger authored Apr 25, 2019
2 parents f7783f7 + 413c4f3 commit 2f821c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package scala.collection.compat

import scala.collection.generic.CanBuildFrom
import scala.collection.{Iterable, mutable}
import scala.collection.mutable

/** Builds a collection of type `C` from elements of type `A` when a source collection of type `From` is available.
* Implicit instances of `BuildFrom` are available for all collection types.
Expand All @@ -23,16 +23,14 @@ import scala.collection.{Iterable, mutable}
* @tparam C Type of collection (e.g. `List[Int]`, `TreeMap[Int, String]`, etc.)
*/
trait BuildFrom[-From, -A, +C] extends Any {

def fromSpecificIterable(from: From)(it: Iterable[A]): C
def fromSpecific(from: From)(it: IterableOnce[A]): C

/** Get a Builder for the collection. For non-strict collection types this will use an intermediate buffer.
* Building collections with `fromSpecificIterable` is preferred because it can be lazy for lazy collections. */
* Building collections with `fromSpecific` is preferred because it can be lazy for lazy collections. */
def newBuilder(from: From): mutable.Builder[A, C]

@deprecated("Use newBuilder instead of apply()", "2.13.0")
@deprecated("Use newBuilder() instead of apply()", "2.13.0")
@`inline` def apply(from: From): mutable.Builder[A, C] = newBuilder(from)

}

object BuildFrom {
Expand All @@ -41,7 +39,7 @@ object BuildFrom {
implicit def fromCanBuildFrom[From, A, C](
implicit cbf: CanBuildFrom[From, A, C]): BuildFrom[From, A, C] =
new BuildFrom[From, A, C] {
def fromSpecificIterable(from: From)(it: Iterable[A]): C = (cbf(from) ++= it).result()
def fromSpecific(from: From)(it: IterableOnce[A]): C = (cbf(from) ++= it).result()
def newBuilder(from: From): mutable.Builder[A, C] = cbf(from)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,10 @@ class BuildFromTest {
Map: BuildFrom[_, (Int, String), Map[Int, String]]
SortedSet: BuildFrom[_, Int, SortedSet[Int]]
SortedMap: BuildFrom[_, (Int, String), SortedMap[Int, String]]

// Implement BuildFrom
class MyBuildFrom[From, A, C] extends BuildFrom[From, A, C] {
def fromSpecific(from: From)(it: IterableOnce[A]): C = ???
def newBuilder(from: From): Builder[A, C] = ???
}
}

0 comments on commit 2f821c3

Please sign in to comment.