Is it possible to do variadic templates in Kotlin?

Viewed 191

That is, is there a type safe way to make a function f that can receive an arbitrary number of parameters and a function that receives them in bulk and would work in a type safe fashion?

Say, let's have

fun<R, T1> func(f: (T1)->R, arg1: T1) { f(arg1) }
fun<R, T1, T2> func(f: (T1, T2)->R, arg1: T1, arg2: T2) { f(arg1, arg2) }
...

except that there is only a single body for func that supports an arbitrary number of such arguments?

I can accept compile time processing if there is no other way (e.g. generate the specific number of arguments in use automatically, or use a script to manually generate for up to, say, 20 arguments)

1 Answers

I don't think this is possible from "within the language". As you are OK with generating sources, Kotlin Poet may be of use.

Related