Skip to content

immutable maps have a dangerous remove method #11189

@Ichoran

Description

@Ichoran

Immutable maps have a new method called remove that is dangerous when combined with mutable maps which have a side-effecting remove. If you get your map type wrong, you will silently fail to modify your map with no error.

Welcome to Scala 2.13.0-20181002-132957-0036e7a (OpenJDK 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.

scala> val m = Map(3 -> "wish")
m: scala.collection.immutable.Map[Int,String] = Map(3 -> wish)

scala> m.remove(3)
res0: scala.collection.immutable.Map[Int,String] = Map()

scala> val mm = collection.mutable.Map(3 -> "wish")
mm: scala.collection.mutable.Map[Int,String] = HashMap(3 -> wish)

scala> mm.remove(3)
res1: Option[String] = Some(wish)

scala> mm
res2: scala.collection.mutable.Map[Int,String] = HashMap()

Immutable maps cannot have a remove method since the mutable map return value will often be ignored and hence the different return types cannot be used to catch the error.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions