data class Form(
..
val fields: Fields
) {
data class Fields(
val personalNumber: InputField,
...
)
}
data class InputField(
val value: String,
..
}
this is my model
I want to update value
obviously its a copy of a copy of a copy, which is ugly, I want to stuff into a extension
private fun <T> Form.updatePersonalNumberValue(value: String): Form {
val field = fields.personalNumber.copy(value = value)
copy(fields = fields.copy(personalNumber = field)
}
works but obviously doesn't scale as I have 20 fields, is there a way to make this generic somehow?
private fun Form.updateFieldValue(selector: (Form.Fields) -> InputField, value: String) {
I was thinking something like this, a selector lambda
val field = selector(fields)
however how do I assign it back to the
Form
? So seems like a dead end. It seems I can only do
Fields -> Fields
lambda at best
Is this really the best you can do in kotlin?
form.updateFields {
copy(
personalNumber = personalNumber.copy(
value = "new personal number"
)
)
}
TLDR; is there a way to generically select a copy parameter?