How can I map a list of strings to the properties ...
# stdlib
s
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.
Copy code
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
Copy code
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>() }
e
either reflection or compile-time processing (e.g. with https://github.com/hfhbd/kotlinx-serialization-csv) is required
s
Thanks for the repo!
If I may say, you are really helpful around here! Thank you for being such a helpful (besides for also being very competent) part of the community!
9