I am experimenting with Lenses in Kotlin, and I was wondering if there is an elegant way to change multiple attributes at the same time for one object. Let's say my domain looks something like this:
@optics
data class Parameters(
val duration: Int,
val length: Int) {
companion object
}
@optics
data class Calculation(
val product: String
val parameters: Parameters) {
companion object
}
thanks to the @optics
annotations, editing single fields is easy to do:
val calculation = Calculation(product = "prod", Parameters(duration = 10, length = 15))
Calculation.product.modify(calculation) { selectedProduct }
Calculation.parameters.duration(calculation) { newDuration() }
Calculation.parameters.length(calculation) { 10 }
These lenses work perfectly in isolation, but what is the right pattern to use when I want to apply the three transformations at once? I can use a var
and just overwrite calculation
every time, but that does not feel very idiomatic to me.