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

Kev

03/22/2024, 12:37 PM
I’m trying to use optics to modify a property on a data object but it’s not having it.
Copy code
@optics
data class Foo(val id: UUID, val name: String, val bars: List<Bar>) {
  companion object
}

@optics
data class Bar(val id: UUID, val name: String, val visited: Boolean) {
  companion object
}
I get as far as
Every.list<Foo>().bars.every(Every.list())
and it bombs out on the second every method with
Overload resolution ambiguity. All these functions match.
I’m not sure what exactly I’m doing wrong and the resources on the website don’t go into detail with traversing lists within lists (I don’t see why it should be any different, I must be missing something obvious).
I’ve come up with this but it doesn’t feel optimal.
Copy code
fun List<Foo>.visit(id: UUID): List<Foo> =
  Every.list<Foo>().bars.modify(this) { bar ->
    bar.map { Bar(it.id, it.name, it.id == id) }
  }