How to make a builder for a Kotlin data class with many immutable properties

Viewed 6115

I have a Kotlin data class that I am constructing with many immutable properties, which are being fetched from separate SQL queries. If I want to construct the data class using the builder pattern, how do I do this without making those properties mutable?

For example, instead of constructing via

var data = MyData(val1, val2, val3)

I want to use

builder.someVal(val1)
// compute val2
builder.someOtherVal(val2)
// ... 
var data = builder.build()

while still using Kotlin's data class feature and immutable properties.

4 Answers
Related