https://kotlinlang.org logo
#arrow
Title
# arrow
f

Fred Friis

03/12/2024, 4:19 PM
we need to change an attribute that's pretty deep in a nested structure of immutable data classes, is there no simpler/more elegant way to do this in Kotlin? or do we have to use lenses or whatever?
Copy code
kotlin
        return taxableOrder
            .copy(
                checks = taxableOrder
                    .checks
                    ?.map { taxableCheck ->
                        taxableCheck.copy(selections = taxableCheck
                                .selections
                                ?.map { taxableSelection ->
                                    taxableSelection.copy(taxCategory = TaxableSelection.TaxCategory.HOT_FOOD) //this is really the only thing we want to change
                                }
                        )
                    }
            )
// but we still have to map and copy every step on the way into that nested attribute 🫠
s

Sam

03/12/2024, 4:23 PM
I'm still holding out hope for https://youtrack.jetbrains.com/issue/KT-44653
2
a

Alejandro Serrano.Mena

03/12/2024, 4:51 PM
this is exactly what optics are for! https://arrow-kt.io/learn/immutable-data/
👍 2
arrow 1
mind blown 1
s

simon.vergauwen

03/13/2024, 6:27 AM
@Sam all my prayers go towards a real immutable DSL. Like the Optics DSL but 1st class, and better 😂 Oof all comments are 3+ years old. Has it been that long since this was announced/shared 🤣 Jeez time flies.