Skip to content
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

scala.collection.Map.foreachEntry backport for 2.11/2.12 #341

Merged
merged 1 commit into from
Jun 15, 2020
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 @@ -177,6 +177,10 @@ private[compat] trait PackageShared {
type IterableOnce[+X] = c.TraversableOnce[X]
val IterableOnce = c.TraversableOnce

implicit def toMapExtensionMethods[K, V](
self: scala.collection.Map[K, V]): MapExtensionMethods[K, V] =
new MapExtensionMethods[K, V](self)

implicit def toMapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](
self: IterableView[(K, V), C]): MapViewExtensionMethods[K, V, C] =
new MapViewExtensionMethods[K, V, C](self)
Expand Down Expand Up @@ -393,6 +397,14 @@ class Tuple2ZippedExtensionMethods[El1, Repr1, El2, Repr2](
new Tuple3Zipped((self.colls._1, self.colls._2, t3))
}

class MapExtensionMethods[K, V](private val self: scala.collection.Map[K, V]) extends AnyVal {

def foreachEntry[U](f: (K, V) => U): Unit = {
self.foreach { case (k, v) => f(k, v) }
}

}

class MapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]](
private val self: IterableView[(K, V), C])
extends AnyVal {
Expand Down
29 changes: 29 additions & 0 deletions compat/src/test/scala/test/scala/collection/MapTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package test.scala.collection

import org.junit.Test
import org.junit.Assert._

import scala.collection.compat._

class MapTest {

@Test
def foreachEntry: Unit = {
val map = Map("a" -> 1, "b" -> 2, "c" -> 3)
var copy = Map.empty[String, Int]
map.foreachEntry((k, v) => copy += k -> v)
assertEquals(map, copy)
}

}