How can I map a list of strings to the properties of a data class? Any way without reflection? Every string corresponds to the declared property at the string’s index, e.g.
data class Foo(a: String, b: String)
val foo: Foo = listOf("one", "two").mapToClass<Foo>()
There wouldn’t necessarily be type-safety, but it would be a cool language feature. It comes up often when parsing CSVs. I end up doing
Files.readAllLines(path).map {
it.split(",").let {
Foo(
it[0],
it[1]
...
)
}
}
Instead of being able to do
Files.readAllLines(path).toCSV<Foo>()
or
Files.readAllLines(path).map { it.split(",").mapToClass<Foo>() }