Spread operator not working on vararg indexed access operator


#1

When using vararg as indexed access operator argument, we can only use explicit get function rather than square brackets. The following code shows this:

class A {
    operator fun get(vararg index: Int): Int {
        return 0
    }
}

val a = A()
val b = a[1, 2, 3] // OK
val c = intArrayOf(1, 2, 3)
val d = a.get(*c) // OK
val e = a[c] //Error: Kotlin: Type mismatch: inferred type is IntArray but Int was expected
val f = a[*c]/*Error: Kotlin: None of the following functions can be called with the arguments supplied: 
public final operator fun times(other: Byte): Int defined in kotlin.Int
public final operator fun times(other: Double): Double defined in kotlin.Int
public final operator fun times(other: Float): Float defined in kotlin.Int
public final operator fun times(other: Int): Int defined in kotlin.Int
public final operator fun times(other: Long): Long defined in kotlin.Int
public final operator fun times(other: Short): Int defined in kotlin.Int
*/

Is it a compiler bug?


#2

This is a compiler limitation. :slight_smile: Please file a YouTrack issue.


#3

OK. I’ll do that.


#4

In case someone’s interested, it’s this issue