vararg parameter in a Kotlin lambda

I’m afraid this is not possible. The following demonstrates the type of a function with vararg. A vararg parameter is represented by an Array:

fun withVarargs(vararg x: String) = Unit

val f: KFunction1<Array<out String>, Unit> = ::withVarargs

This behavior is also suggested in the docs:

Inside a function a vararg-parameter of type T is visible as an array of T, i.e. the ts variable in the example above has type Array<out T>.

So you had to use an Array<String> in your example. Instead of writing your function as a lambda, you may use an ordinary function that allows to use vararg and make the call look as desired:

fun f(vararg strings: String) = strings.forEach(::println)

f("a","b","c")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)