@@ -7,15 +7,16 @@ title: Scala 2.13’s Collections
7
7
8
8
One more article about the standard collections, really? Indeed, during the last
9
9
18 months a lot of work has been done on the collections side and we’ve published
10
- several blog articles and given several talks to the explain various changes or
10
+ several blog articles and given several talks to explain the various changes or
11
11
challenges we were facing. This article summarizes ** what is going
12
12
to change from an end-user perspective** .
13
13
14
- Even if you have thoroughly followed our previous blog posts and talks, this article
15
- gives you a good overview for summarizing the changes to your colleagues.
14
+ In case you’ve thoroughly followed our previous blog posts and talks, you might
15
+ not learn much from this article. Otherwise, this is the perfect opportunity
16
+ to catch up on the topic in a few minutes!
16
17
17
- The next section presents the internal changes in the collections implementation
18
- that might have some visible impact on the surface. Then, I will show why I think
18
+ The next section presents the changes that are internal to the collections implementation
19
+ but that might have some visible impact on the surface. Then, I will show why I think
19
20
that the removal of ` CanBuildFrom ` made the API more beginner friendly. Next, I
20
21
will introduce some new operations available in the collections. Finally, I
21
22
will mention the main deprecations, the motivations behind them, and their
@@ -49,9 +50,9 @@ deprecated in favor of `LazyList` (see the last section).
49
50
50
51
I think the most visible change for end-users is that transformation operations
51
52
don’t use ` CanBuildFrom ` anymore. I believe this will be quite visible despite our previous
52
- efforts to * hide* ` CanBuildFrom ` from the API documentation in the current collections.
53
+ efforts to * hide* ` CanBuildFrom ` from the API documentation of the collections.
53
54
Indeed, if you take a look at the
54
- [ current ` List ` API] ( /api/2.12.6/scala/collection/immutable/List.html ) , the signature
55
+ [ old 2.12 ` List ` API] ( /api/2.12.6/scala/collection/immutable/List.html ) , the signature
55
56
shown for the ` map ` operation does not mention ` CanBuildFrom ` :
56
57
57
58
![ there is no CanBuildFrom] ( /resources/img/blog/scaladoc-list-map.png )
@@ -85,9 +86,9 @@ a single operation implementation, and `CanBuildFrom` was used to abstract over
85
86
the various possible return types.
86
87
87
88
In the new collections we wanted to have simpler type signatures so that we
88
- can shoulder their actual form in the API documentation and auto-completion
89
+ can show their actual signature in the API documentation, and auto-completion
89
90
provided by IDEs is not scary. We achieve that by using overloading, as
90
- explained in more details in
91
+ explained in more detail in
91
92
[ this blog article] ( /blog/2017/05/30/tribulations-canbuildfrom.html ) .
92
93
93
94
In practice, this means that the new ` TreeMap ` has three overloads of the
@@ -98,7 +99,7 @@ In practice, this means that the new `TreeMap` has three overloads of the
98
99
These type signatures are the actual ones and they essentially translate
99
100
“in types” what I’ve written above about the possible result types of ` map `
100
101
according to the type of elements returned by the transformation function ` f ` .
101
- I believe that the new API is simpler to understand.
102
+ We believe that the new API is simpler to understand.
102
103
103
104
## New And Noteworthy
104
105
@@ -107,7 +108,7 @@ present some of them.
107
108
108
109
### ` groupMap `
109
110
110
- A common pattern with the current collection is to use ` groupBy `
111
+ A common pattern with the old 2.12 collections is to use ` groupBy `
111
112
followed by ` mapValues ` to transform the groups. For instance,
112
113
this is how we can index the names of a collection of users by
113
114
their age:
@@ -161,14 +162,15 @@ users
161
162
## Deprecations For Less Confusion
162
163
163
164
A consequence of cleaning and simplifying the collections framework
164
- is that several types or operations have been deprecated.
165
+ is that several types or operations have been deprecated in Scala 2.13
166
+ and will be removed in 2.14.
165
167
166
168
### ` Iterable ` Is The Top Collection Type
167
169
168
170
We felt that having a distinction between ` Traversable ` and ` Iterable ` was not
169
171
worth it, so we removed ` Traversable ` (it is now an alias to ` Iterable[A] ` ).
170
172
171
- ` IterableOnce [A]` is now the collection type at the top of the hierarchy.
173
+ ` Iterable [A]` is now the collection type at the top of the hierarchy.
172
174
Its only abstract member is ` def iterator: Iterator[A] ` .
173
175
174
176
### ` LazyList ` Is Preferred Over ` Stream `
@@ -180,7 +182,7 @@ the head and the tail are lazy, whereas in `Stream` only the tail is lazy.
180
182
181
183
### Insertion And Removal Operations Are Not Available On Generic Collections
182
184
183
- In the current framework, the ` scala.collection.Map ` type has a ` + ` and a ` - ` operations
185
+ In the old 2.12 framework, the ` scala.collection.Map ` type has a ` + ` and a ` - ` operations
184
186
to add and remove entries. The semantics of these operations is to return a new collection
185
187
with the added or removed entries, without changing the source collection.
186
188
@@ -191,8 +193,8 @@ has `+` and `+=`, as well as `-` and `-=`.
191
193
192
194
Having all these operations can be handy in some cases but can also introduce confusion. If you want
193
195
to use ` + ` or ` - ` , then you probably wanted to use an immutable collection type in the first place…
194
- Another example is the ` updated ` operation, which is available on mutable ` Map ` but returns a new
195
- collection.
196
+ Consequently, ` + ` , ` - ` and ` updated ` have been moved from ` scala.collection. Map` to ` scala.collection.immutable.Map ` ,
197
+ and ` + ` and ` - ` have been moved from ` scala. collection.Set ` to ` scala.collection.immutable.Set `
196
198
197
199
We think that by deprecating these insertion and removal operations from generic collection
198
200
types and by having distinct operations between the ` mutable ` and ` immutable ` branches we make
0 commit comments