Reputation: 39260
Is it possible to implement the spread operator on other classes in the same way you can with other operators like +
, for example:
class Demo{
operator fun plus(i:Int):Demo {
...
}
}
Upvotes: 0
Views: 750
Reputation: 5300
No you can`t. The spread operator is not matched by any function therefore it can not be overloaded in Kotlin.
When looking at the bytecode created by the compiler one can see, that vararg
is compiled to an array. The spread operator just creates a copy of that array.
For example:
fun test(vararg strings: String) {
}
fun main() {
val params = arrayOf("1", "2")
test(*params)
}
compiles to:
INVOKESTATIC java/util/Arrays.copyOf ([Ljava/lang/Object;I)[Ljava/lang/Object;
CHECKCAST [Ljava/lang/String;
INVOKESTATIC CoroutineTestKt.test ([Ljava/lang/String;)V
Upvotes: 1