Generics 将某些值连接到vararg参数数组

Generics 将某些值连接到vararg参数数组,generics,compiler-errors,kotlin,variadic-functions,Generics,Compiler Errors,Kotlin,Variadic Functions,我一直试图在下面的函数中填补空白(val args=…)。尝试了我能想到的一切,找到了一个相当复杂的解决方案。我觉得有更好的方法,请建议一种更适合的方法。我是否缺少一个概念/操作员/乐趣/类型? class Result interface Runner { fun execute(vararg tasks: String): Result } fun Runner.debugExec(vararg tasks: String): Result { // red: compile errors

我一直试图在下面的函数中填补空白(
val args=…
)。尝试了我能想到的一切,找到了一个相当复杂的解决方案。我觉得有更好的方法,请建议一种更适合的方法。我是否缺少一个概念/操作员/乐趣/类型?

class Result
interface Runner { fun execute(vararg tasks: String): Result }

fun Runner.debugExec(vararg tasks: String): Result {
// red: compile errors, mostly 
    //val args = tasks + "--debug" 
    //val args: Array<String> = tasks + "--debug"
    //val args: Array<out String> = tasks + "--debug"
    //val args = tasks + arrayOf("--debug")
    //val args = tasks + arrayOf<String>("--debug")
    //val args = tasks + listOf("--debug")
    //val args = tasks + (arrayOf("--debug") as Array<out String>)
    //val args = tasks + arrayOf<out String>("--debug") // Projections are not allowed here
    //val args = tasks.toList() + "--debug" // spread operator doesn't work
    //val args = tasks.plusElement("--debug") // cannot infer
    //val args = "--debug" + tasks // it's .toString() and spread operator doesn't work
// yellow: works, but warns
    //val args = (tasks as Array<String>) + "--debug" // unchecked cast
// green: but we must be able to do better, it's kotlin after all! 
    //val args = (tasks.toList() + "--debug").toTypedArray() // too many method calls
    println(args)
    return this.execute(*args)
}
类结果
接口运行程序{fun execute(vararg tasks:String):Result}
fun Runner.debugExec(vararg任务:字符串):结果{
//红色:主要是编译错误
//val args=tasks+“--debug”
//val args:Array=tasks+“--debug”
//val args:Array=tasks+“--debug”
//val args=tasks+arrayOf(“--debug”)
//val args=tasks+arrayOf(“--debug”)
//val args=tasks+listOf(“--debug”)
//val args=tasks+(arrayOf(“--debug”)作为数组)
//val args=tasks+arrayOf(“--debug”)//此处不允许投影
//val args=tasks.toList()+“--debug”//spread运算符不起作用
//val args=tasks.plusElement(“--debug”)//无法推断
//val args=“--debug”+任务//它的.toString()和spread运算符不起作用
//黄色:有效,但警告
//val args=(任务作为数组)+“---debug”//unchecked cast
//格林:但我们必须做得更好,毕竟是科特林!
//val args=(tasks.toList()+“--debug”).toTypedArray()//方法调用太多
println(args)
返回此参数。执行(*args)
}
我经常遇到的编译错误如下:

None of the following functions can be called with the arguments supplied.
Array<T>.plus(T)
  where T cannot be inferred for
    operator fun <T> Array<T>.plus(element: T): Array<T> defined in kotlin.collections
Array<out String>.plus(Array<out String>)
  where T = CapturedTypeConstructor(out String) for
    operator fun <T> Array<T>.plus(elements: Array<out T>): Array<T> defined in kotlin.collections
Array<out String>.plus(Collection<String>)
  where T = CapturedTypeConstructor(out String) for
    operator fun <T> Array<T>.plus(elements: Collection<T>): Array<T> defined in kotlin.collections
使用提供的参数无法调用以下函数。
数组.plus(T)
式中,T不能推断为
运算符fun Array.plus(元素:T):在kotlin.collections中定义的数组
数组.plus(数组)
其中T=的CapturedTypeConstructor(输出字符串)
运算符fun Array.plus(元素:数组):在kotlin.collections中定义的数组
数组.plus(集合)
其中T=的CapturedTypeConstructor(输出字符串)
运算符fun Array.plus(元素:集合):在kotlin.collections中定义的数组

注意:
返回这个。如果没有打印,execute(*tasks,“--debug”)
可以工作,和/或可以接受代码重复。

这是泛型问题。Kotlin中的数组是不变的,而Java则不是这样

文件说:

fun asList(vararg ts:T)
“在函数内部,
vararg
-类型为
T
的参数作为
T
的数组可见,即上面示例中的ts变量具有类型为
array

数组
只能作为
T
的生产者,不能添加元素。
+
映射到的运算符仅在
数组
上定义,即没有
out
修饰符

您可以让该方法接受tasks:Array,它不使用任何投影,也允许将数组用作使用者。

Aha!找到了一个“更好”的解决方案,尽管它没有使用
+

val args = arrayOf(*tasks, "--debug")
的澄清使我再次检查了声明,我注意到参数是


它强制自定义部分位于前面,这在我的情况下是有效的,但一般来说可能不起作用。

在我的阅读中,我试图以这种方式使用
tasks
,因此它“生成”传入数组中的所有值,作为添加新元素的新数组的基础。数组是不可变的,所以
.plus
不是真正的加法,是吗?当然,但是操作符是在
Array
上定义的,没有
out
修饰符。这一个同样有效:val args=tasks.asList().toTypedArray()+“--debug”感谢您的指导思想:
val args = arrayOf(*tasks, "--debug")
public operator fun <T> Array<T>.plus(elements: Array<out T>): Array<T>
val args =  arrayOf("--debug") + tasks