Hi 👋 Arrow newbie here. I'm trying to modify deeply nested immutable data classes which sounds exactly like what Optics are for but I'm having a hard time wrapping my head around it. Is there a sample of how to do this somewhere? Ideally something that does the equivalent of the (simple) example below?
Copy code
// How do I do this with Optics?
val user = user.copy(
address = user.address.copy(
city = newCity
)
)
👀 1
t
Ties
11/18/2021, 3:25 PM
Copy code
val addressLens: Lens<User, Adress> = Lens(
get = { it.address},
set = { s : User, v : Adress> -> s.copy(address= v) }
)
val cityLens: Lens<Adress, City> = Lens(
get = { it.city},
set = { s : Adress, v : City> -> s.copy(city= v) }
)
val adressCity = addressLens.compose(cityLens)
adressCity.modify(user) { newCity }
m
mbonnin
11/18/2021, 3:27 PM
Yikes, that's more code 😅 .
t
Ties
11/18/2021, 3:27 PM
yes 😛 but arrow can generate this for you!
👀 1
m
mbonnin
11/18/2021, 3:28 PM
Ah!
t
Ties
11/18/2021, 3:29 PM
you can do this with your dataclasses:
Copy code
@optics data Person(...) { companion object }
so add an @optics annotation, and add a companion object and make sure you have the optics maven/gradle plugin configured
🙏 1
m
mbonnin
11/18/2021, 3:33 PM
Thanks !
t
Ties
11/18/2021, 3:35 PM
after generating it will be:
User.adress.city.modify(user){ newCity }