Can the lambda of kotlin be a variable parameter function?

want to implement a function similar to that in js


require(["a", "b", "c"], function(a, b, c){



})

ask, how is it written as a parameter in a lambda?

Mar.02,2021

cannot be written. Refer to https://stackoverflow.com/que.


unfortunately cannot be implemented. As mentioned in the question and answer of StackOverflow provided by codegoose, kotlin's lambda does not provide support for the vararg modifier for the time being

to implement the function definition you need, here are two similar implementations I know

// 
interface Cry {
    fun crying(vararg x : String)
}

private fun require(vararg config: String, cry: Cry) {
    cry.crying(*config)
}

fun test1() {
    val config = arrayOf("1", "2")
    require(*config, cry = object : Cry {
        override fun crying(vararg x: String) = x.forEach(::println)
    })
}

// arrayOfvarargArray<out String>
private fun require(vararg config: String, block : (Array<out String>) -> Unit) {
    block(config)
    block(arrayOf("1", "2", "3"))
}

fun test2() {
    val config = arrayOf("1", "2")
    require(*config, block = { it.forEach(::println)})
}
Menu