Using `for` loops and inline classes together can lead to confusing behavior

// Kotlin 1.4.21

fun main() {
    // NOTE: values.size is greater than 3.
    val values = doubleArrayOf(1.0, 2.0, 3.0, 4.0)

    println("InlineDouble3")
    for (i in InlineDouble3(values)) {
        println(i)
    }

    println("NonInlineDouble3")
    for (i in NonInlineDouble3(values)) {
        println(i)
    }
}

inline class InlineDouble3(val values: DoubleArray) {
    operator fun iterator(): DoubleIterator = IteratorImpl(values)
}

class NonInlineDouble3(val values: DoubleArray) {
    operator fun iterator(): DoubleIterator = IteratorImpl(values)
}

// This iterator returns the first 3 elements of this.values
private class IteratorImpl(private val values: DoubleArray) : DoubleIterator() {
    private var index = 0

    override fun hasNext(): Boolean = index < 3

    override fun nextDouble(): Double = values[index++]
}

Result:

InlineDouble3
1.0
2.0
3.0
4.0
NonInlineDouble3
1.0
2.0
3.0

The operator function iterator(), which is declared in an inline class, is not used by for loops.

Is this a bug? Or are inline classes designed to work this way?

EDIT: Nice! Removing the long part of this reply to keep things readable

Looks like this would be runnable. Why not use a runnable example (which can be edited right here on the forum) :slight_smile:. Try editing the example below by changing only the “inline” modifier to enable/disable the behavior difference.

2 Likes

Thanks, I edited my previous post. It’s a nice feature!

Looks like a bug to me. I took the liberty to create an issue for this on the kotlin bug tracker:
https://youtrack.jetbrains.com/issue/KT-44529

1 Like

Thank you so much. I’ll watch for updates.

Looks like this issue is already fixed in the new IR backend, which is currently in Alpha:
https://kotlinlang.org/docs/reference/whatsnew14.html#new-jvm-ir-backend

4 Likes