Skip to content

The old mutate the collection while iterating trick #12009

Open
@som-snytt

Description

@som-snytt

reproduction steps

  • using Scala?
scala 2.13.2> val xs = collection.mutable.ArrayBuffer(1,2,3)
val xs: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)

scala 2.13.2> val it = xs.iterator
val it: Iterator[Int] = <iterator>

scala 2.13.2> it.hasNext
val res0: Boolean = true

scala 2.13.2> xs.clear()

scala 2.13.2> it.hasNext
val res2: Boolean = true

scala 2.13.2> it.next()
val res3: Int = 0

scala 2.13.2> it.to(List)
val res4: List[Int] = List(null, null)

scala 2.13.2> xs.size
val res5: Int = 0

problem

Documentation should tell me not to do that.

It would be nice if the iterator told me there was a problem. It's just using indexes from the view.

See #8154

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions