Skip to content

Commit 80aa1cd

Browse files
authored
Merge pull request scala/scala#9083 from lrytz/smallCleanups
[nomerge] small cleanups
2 parents 5a5cbcd + 759c6d8 commit 80aa1cd

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

library/src/scala/collection/immutable/HashMap.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ sealed class HashMap[A, +B] extends AbstractMap[A, B]
187187
this.merge0(thatHash, 0, concatMerger[A, B])
188188
case that =>
189189
var result: HashMap[A, B] = this
190-
that.asInstanceOf[GenTraversableOnce[(A, B)]] foreach { case kv: (_, _) => result = result + kv }
190+
that.asInstanceOf[GenTraversableOnce[(A, B)]].foreach(result += _)
191191
result
192192
}
193193
castToThat(result)
@@ -212,9 +212,9 @@ sealed class HashMap[A, +B] extends AbstractMap[A, B]
212212
val merger = HashMap.concatMerger[A, B].invert
213213
val result: HashMap[A, B] = that match {
214214
case thatHash: HashMap[A, B] =>
215-
this.merge0(thatHash, 0, HashMap.concatMerger[A, B].invert)
215+
this.merge0(thatHash, 0, merger)
216216

217-
case that:HasForeachEntry[A, B] =>
217+
case that: HasForeachEntry[A, B] =>
218218
object adder extends Function2[A, B, Unit] {
219219
var result: HashMap[A, B] = HashMap.this
220220
override def apply(key: A, value: B): Unit = {
@@ -231,12 +231,15 @@ sealed class HashMap[A, +B] extends AbstractMap[A, B]
231231
result = result.updated0(key, computeHash(key), 0, kv._2, kv, merger)
232232
}
233233
}
234-
that.asInstanceOf[scala.Traversable[(A,B)]] foreach adder
234+
that.asInstanceOf[GenTraversableOnce[(A,B)]] foreach adder
235235
adder.result
236236
}
237237
castToThat(result)
238238
}
239239
}
240+
241+
// These methods exist to encapsulate the `.asInstanceOf[That]` in a slightly safer way -- only suitable values can
242+
// be cast and the type of the `CanBuildFrom` guides type inference.
240243
private[this] def castToThat[C, That](m: HashMap[A, B])(implicit bf: CanBuildFrom[HashMap[A, B], C, That]): That = {
241244
m.asInstanceOf[That]
242245
}

0 commit comments

Comments
 (0)