// 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?