This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Modifying current slot using foreach  RSS feed

 
Leonardo Crespo
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. I read in MZ Tiger Study guide the following:

"Restrictions on the enhanced for loop:
- CANNOT be used to modify the current slot in a collect or an array. It is not usable for loops where you need to replace elements in a list or array as you traverse it".

However, i managed to compile an run this successfully:



Why?

Thank you.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you have done is modified the local reference variable name. If you run the following code you will see that the collection has not been changed.

 
Leonardo Crespo
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, thank you Barry!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!