Pin*_*rya 3 java android kotlin kotlin-interop
我正在尝试接受 vararg 参数作为 Kotlin 中的函数参数,并尝试将其传递给另一个带有 vararg 参数的函数。但是,这样做会给我带来编译时错误type mismatch: inferred type is IntArray but Int was expected
。
科特林:
fun a(vararg a: Int){
b(a) // type mismatch inferred type is IntArray but Int was expected
}
fun b(vararg b: Int){
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在 Java 中尝试相同的代码,它就会起作用。
爪哇:
void a(int... a) {
b(a); // works completely fine
}
void b(int... b) {
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
*
只需在传递的参数(扩展运算符)前面加上一个,即
fun a(vararg a: Int){
// a actually now is of type IntArray
b(*a) // this will ensure that it can be passed to a vararg method again
}
Run Code Online (Sandbox Code Playgroud)
另请参阅:Kotlin 函数参考 #varargs
归档时间: |
|
查看次数: |
866 次 |
最近记录: |