Jire
Jire

Reputation: 10290

Kotlin: Is it no longer possible to iterate a map with destructured Entry? Bug?

It seems like with the latest Kotlin update you can no longer iterate through maps with destructured Entry. For example:

val map = HashMap<Int, String>()

for ((i, s) in map) { // compiler error here
    ... // code
}

enter image description here

The compiler error is For-loop range must have an iterator() method

This feature previously worked as expected, iterating through each entry in the map. I have also tried to use map.entries and map.entrySet() (deprecated) and with these:

enter image description here

What is the idiomatic way to iterate through a map now? Is this a bug?

Upvotes: 4

Views: 3286

Answers (2)

hotkey
hotkey

Reputation: 148109

Looks like a bug in IDE and Gradle plugins. I tried the following:

  • Created a clean Gradle project with Kotlin 4584 and pasted your code. The bug was there, the same to your occurence.
  • Changed Kotlin version to the previous one, 4583, and reimported the project. The code compiled well.
  • Changed Kotlin version back to 4584, and the bug didn't show itself, the code compiled successfully.

No following changes could let me reproduce the bug in this project, but I could in a new one.

Upvotes: 1

Jire
Jire

Reputation: 10290

Oddly IntelliJ offered me to "update" to Kotlin plugin 2428 which caused this.

Downloading off JetBrain's plugin repository (Kotlin plugin here: http://plugins.jetbrains.com/plugin/6954?pr=idea) and choosing Install from disk... in IntelliJ fixed the problem.

Upvotes: 0

Related Questions