I have a data class namely:
data class Entry(var name: String, var address: String, var phoneNo: String,
private val amt: String, var remark: String)
And I have a String Array: val data = arrayOf("x", "y", "z", "a", "b")
I'd like to pass the data array as a parameter when creating a new instance of Entry. I don't want to do it like this: val entry = Entry(data[0], data[1], data[2], data[3], data[4])
I've tried using the spread operator: Entry(*data) but it gives me an error saying the spread operator is not allowed here. This is because the parameter isn't a vararg. Also, note that varargs aren't allowed in the constructor of data classes.
Is there any way in Kotlin to spread an array to non-variadic functions (functions with a fixed number of arguments), especially if you know that the length of that array will always be the same?
I haven't found an answer to this yet after various searches. Could use some help. Thanks.
Edit: Collections in Kotlin do have component1(), component2()... methods all the way upto component5() for the first 5 elements of the array/list. So this allows one to do a destructuring assignment: val (a, b, c, d, e) = data. And then you can pass them on to the function/constructor: Entry(a, b, c, d, e).
But this is only half a solution because you're limited to the first 5 elements and it's still more verbose than a simple spread operator.