You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/core/collections-migration-213.md
+26-2Lines changed: 26 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ how to cross-build projects with Scala 2.11 / 2.12 and 2.13.
10
10
For an in-depth overview of the Scala 2.13 collections library, see the [collections guide]({{ site.baseurl }}/overviews/collections-2.13/introduction.html). The implementation details of the 2.13 collections are explained in the document [the architecture of Scala collections]({{ site.baseurl }}/overviews/core/architecture-of-scala-213-collections.html).
11
11
12
12
The most important changes in the Scala 2.13 collections library are:
13
+
-`scala.Seq[+A]` is now an alias for `scala.collection.immutable.Seq[A]` (instead of `scala.collection.Seq[A]`). Note that this also changes the type of Scala varargs methods.
13
14
- Transformation methods no longer have an implicit `CanBuildFrom` parameter. This makes the library easier to understand (in source code, Scaladoc, and IDE code completion). It also makes compiling user code more efficient.
14
15
- The type hierarchy is simplified. `Traversable` no longer exists, only `Iterable`.
15
16
- The `to[Collection]` method was replaced by the `to(Collection)` method.
@@ -22,14 +23,38 @@ The most important changes in the Scala 2.13 collections library are:
22
23
- Deprecated collections were removed (`MutableList`, `immutable.Stack`, others)
23
24
- Parallel collections are now in a separate hierarchy in a [separate module](https://github.com/scala/scala-parallel-collections).
24
25
- The `scala.jdk.StreamConverters` object provides extension methods to create (sequential or parallel) Java 8 streams for Scala collections.
25
-
-`scala.Seq` is now an alias for `scala.collection.immutable.Seq` (no longer `scala.collection.Seq`). Note that this also changes the type of Scala varargs methods.
26
26
27
27
## Tools for migrating and cross-building
28
28
29
29
The [scala-collection-compat](https://github.com/scala/scala-collection-compat) is a library released for 2.11, 2.12 and 2.13 that provides some of the new APIs from Scala 2.13 for the older versions. This simplifies cross-building projects.
30
30
31
31
The module also provides [migratrion rules](https://github.com/scala/scala-collection-compat#migration-tool) for [scalafix](https://scalacenter.github.io/scalafix/docs/users/installation.html) that can update a project's source code to work with the 2.13 collections library.
32
32
33
+
## scala.Seq migration
34
+
35
+
In Scala 2.13 `scala.Seq[+A]` is an alias for `scala.collection.immutable.Seq[A]`, instead of `scala.collection.Seq[A]`. This change requires some planning depending on how your code is going to be used.
36
+
37
+
If you're making an application, and simply migrating Scala 2.12 code base to 2.13, it might be ok to keep using `scala.Seq` in your code.
38
+
39
+
If you're making a library intended to be used by other programmers, then using `scala.Seq` or vararg is going to be a breaking change in the API semantics. For example, if there was a function `def orderFood(order: Seq[Order]): Seq[Food]`, previously the library user would have been able to pass in an array of `Order`, but it won't work for 2.13.
40
+
41
+
- if you cross build with Scala 2.12 and want to maintain the API semantics for 2.13 version of your library, or
42
+
- if your library users frequently uses mutable collections such as `Array`
43
+
44
+
you can import `scala.collection.Seq` ("CSeq") explicitly in your code.
45
+
46
+
~~~scala
47
+
importscala.collection.Seq
48
+
~~~
49
+
50
+
In the future when your API is able to break the source compatibility, it might also make sense to migrate towards the `scala.collection.immutable.Seq` ("ISeq") for both Scala 2.12 and Scala 2.13.
51
+
52
+
~~~scala
53
+
importscala.collection.immutable.Seq
54
+
~~~
55
+
56
+
Note that in Scala 2.13 the sequence passed into a vararg as `orderFood(xs: _*)` must also be immutable. This is because the sequence passed into a vararg must conform to`scala.Seq` acoording to [Specification](https://www.scala-lang.org/files/archive/spec/2.12/06-expressions.html#function-applications). Thus if your API exposes varargs, it will be an unavoidable breaking change. This might affect Java interoperability.
57
+
33
58
## What are the breaking changes?
34
59
35
60
The following table summarizes the breaking changes. The "Automatic Migration Rule" column gives the name of the migration rule that can be used to automatically update old code to the new expected form.
@@ -77,7 +102,6 @@ Some classes have been removed, made private or have no equivalent in the new de
77
102
Other notable changes are:
78
103
79
104
-`Iterable.partition` invokes `iterator` twice on non-strict collections and assumes it gets two iterators over the same elements. Strict subclasses override `partition` do perform only a single traversal
80
-
-`scala.Seq[+A]` is now `scala.collection.immutable.Seq[A]` (this also affects varargs methods).
81
105
- Equality between collections is not anymore defined at the level of `Iterable`. It is defined separately in the `Set`, `Seq` and `Map` branches. Another consequence is that `Iterable` does not anymore have a `canEqual` method.
82
106
- The new collections makes more use of overloading. You can find more information about the motivation
83
107
behind this choice [here](http://scala-lang.org/blog/2017/05/30/tribulations-canbuildfrom.html). For instance, `Map.map` is overloaded:
0 commit comments