Kaushalya
01/05/2022, 7:33 AMthan_
01/06/2022, 2:11 PMthan_
01/06/2022, 2:12 PMKaushalya
01/09/2022, 2:43 AMthan_
01/09/2022, 10:04 AMthan_
01/09/2022, 10:12 AMKaushalya
01/09/2022, 3:03 PMjulian
01/09/2022, 5:38 PMEither
data type so that I no longer threw exceptions, but instead modeled all possible function results as types. Then I tried introducing immutability into my app model. And so on...
Hope that helps.Lukasz Kalnik
01/10/2022, 12:12 PMEither
as the wrapper for my API calls made with Retrofit. I wrote a custom Retrofit CallAdapter
to be able to define my API like this:
data class User(val name: String)
interface MyService {
@GET("/user/me")
suspend fun user(): Either<CallError, User>
}
This way I can flatMap
multiple API calls (or even better, put them into an either
block and use the bind
syntax).
There is an open PR to include my Retrofit adapter into Arrow (there are already other adapters, but they still throw exceptions when there is no network, timeout or malformed JSON):
https://github.com/arrow-kt/arrow/pull/2621Kaushalya
01/10/2022, 1:22 PMthan_
01/10/2022, 1:58 PMLukasz Kalnik
01/10/2022, 3:49 PMcopy()
on immutable data under the hood is a problem.
If your app does have to perform lots of data operations that performance starts being important it's worth considering moving this logic to a backend.
Anyway in my case the sole benefit of not having bugs in my code because of data being immutable is a much bigger benefit than any performance penalty (which in practice I didn't observe anyway).
My app is basically a REST client with some UI logic, like most typical mobile apps nowadays.Lukasz Kalnik
01/10/2022, 3:50 PMKaushalya
01/14/2022, 12:50 PMRegarding immutable data structures. Do you perform thousands mutations per second?No, but AFIAK immutable data structures have memory overhead,
There is an overhead of creating a new object for every "mutation" (except for collections 😕 ).@than_ I thought immutability is achieved through structural sharing
But on the other hand detection whether object changed becomes trivial and thus significantly faster. So you can see actually performance increase.Can you please explain this?
Kaushalya
01/14/2022, 12:55 PMLukasz Kalnik
01/14/2022, 1:03 PMthan_
01/14/2022, 1:08 PMI thought immutability is achieved through structural sharingIt is but you have to (by definition) create new instance of object for every change. That's the overhead. The unchanged content gets shared. Unfortunately collections don't perform structural sharing. But you can use kotlinx.immutable.collections for that.
But on the other hand detection whether object changed becomes trivial and thus significantly faster. So you can see actually performance increase.Since every change creates new instance you can just check for referential equality.
than_
01/14/2022, 1:13 PMFrom my experience people greatly overestimate the performance/memory impact on their apps.^that. I've ever had only a single performance issue with immutability and that itself was a workaround to integrate with suboptimally designed 3rd party lib. Do we use mutability to gain a perf increase on some operations on collections? Yes. But from the callsite you won't know that because the mutability is contained.
Lukasz Kalnik
01/14/2022, 1:20 PMthan_
01/14/2022, 1:46 PMthan_
01/14/2022, 1:54 PMrcd27
02/10/2022, 8:27 AMEither
heavily in prod (Android development). That makes all error handling to be in one place - a layer which holds state.