Skip to content

Upgrade BuildFrom to the latest definition from 2.13 #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any test coverage for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test to ensure that a BuildFrom implementation compiles on all versions.

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] = ???
}
}