https://kotlinlang.org logo
a

Ayfri

08/06/2021, 3:57 PM
Hi, why can't I use a vararg argument for a typealias of a function ?
y

Youssef Shoaib [MOD]

08/06/2021, 4:22 PM
Using an
Array<out T>
is equivalent to using
vararg param: T
, and then you can also provide a convenient extension function like this: (playground)
Copy code
typealias VarargFun<E, R> = (items: Array<out E>) -> R
operator fun <E, R> VarargFun<E, R>.invoke(vararg items: E): R = this(items)

fun testVararg(vararg test: Any?) = listOf(*test)

fun main(){
    var test: VarargFun<Any?, List<Any?>> = ::testVararg
    println(test("hello", "world"))
}
a

Ayfri

08/06/2021, 4:30 PM
Nice it works, thanks !
t

turansky

08/06/2021, 5:56 PM
17 Views