inline fun IntBag.forEach(action: (Int) -> Unit): Unit { for (i in indices) action(this.get(i)) } val IntBag.indices: IntRange get() = 0..size() - 1 //note bag size is 595 for (i in bag.indices) { if (i >= 593) { print("value of i is $i, size is ${bag.size()}") } } //prints out 593, 594 and ends bag.forEach { it -> if (it >= 593) { print("value of i is $it, size is: ${bag.size()}") } } //prints out 593, 594, 595(!!!, out of range)