Hi :wave: Arrow newbie here. I'm trying to modify ...
# arrow
m
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
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
Yikes, that's more code ๐Ÿ˜… .
t
yes ๐Ÿ˜› but arrow can generate this for you!
๐Ÿ‘€ 1
m
Ah!
t
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
Thanks !
t
after generating it will be: User.adress.city.modify(user){ newCity }
๐Ÿ˜ 2