https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
u

ursus

03/05/2020, 12:29 AM
Copy code
val newState = state.copy(
	items = state.items.map {
		if (it.id == id) {
			it.copy(
				text = newText
			)
		} else {
			it
		}
	}
)
t

tschuchort

03/05/2020, 10:12 AM
The correct answer is to use optics/lenses and datatype generic programming (à la SYB or generics-sop in Haskell lingo). I think arrow has lenses, although they were quite annoying to use last time I checked. So it may be better to wait a few more months until arrow-meta and such stabilizes and a nicer lens API may be released.
Basically, lenses are composable copy-set methods, such that for
setItem:  State.(Item) -> State
and
setId: Item.(Id) -> Item
you can compose them to a method
setItemId: State.(Id) -> State
.
u

ursus

03/05/2020, 11:20 AM
what would be the syntax ..
item = item.setId
?
t

tschuchort

03/05/2020, 2:16 PM
something like that. I only know them from Haskell
2 Views