Orhan Tozan
03/04/2021, 7:42 PMSatyam Agarwal
03/04/2021, 7:48 PMsuspend, it is just cleverly piggybacking on the functionality suspend provide naturally, which is a huge convenience for arrow. suspend is isomorphic to IO<A>, and thus not only allowing catching side-effects smartly, but also making it lazy.Satyam Agarwal
03/04/2021, 7:48 PM0.12.0, you can just wrap your method with withContext(IO) { yourBlock() }`Satyam Agarwal
03/04/2021, 7:49 PMOrhan Tozan
03/04/2021, 7:50 PMSatyam Agarwal
03/04/2021, 7:50 PMarrow-fx-coroutines library provides either block that handle both suspend () -> A, and suspend () -> Either<Throwable, A> interchangeably.Orhan Tozan
03/04/2021, 7:51 PMSatyam Agarwal
03/04/2021, 7:53 PMBut then the function gets a suspend signature right? And becomes “tagged” as “produces side effects”That is not entirely true. afaik, you would need to return
Either<Throwable, A>, if your function has side affects.
But you can just return A in your case.
f.eks. :
suspend fun myIOPureFun(): A {
return withContext(IO) {
myHeavyComputations()
}
}Satyam Agarwal
03/04/2021, 7:54 PMeither { ... } block. in case something blows up, and you were “sure” that function was pure.Orhan Tozan
03/04/2021, 7:57 PMIn Arrow Fx, we use suspend fun to denote a function that may cause side effects when invoked.
In the example below, println(a : Any): Unit is a side effect because, every time it's invoked, it causes observable effects by interacting with the System.out stream and signals that it produces no useful output by returning Unit.
Satyam Agarwal
03/04/2021, 8:00 PMSatyam Agarwal
03/04/2021, 8:00 PMfun main() { // The edge of our world
val env = Environment()
env.unsafeRunSync { greet() }
}Satyam Agarwal
03/04/2021, 8:00 PMeither { … }Satyam Agarwal
03/04/2021, 8:02 PMEither datatype entirely. Its upto you how you want to do error handling.Orhan Tozan
03/04/2021, 8:05 PMSatyam Agarwal
03/04/2021, 8:09 PMOrhan Tozan
03/04/2021, 8:10 PMOrhan Tozan
03/04/2021, 8:11 PMSatyam Agarwal
03/04/2021, 8:11 PMSatyam Agarwal
03/04/2021, 8:13 PMSatyam Agarwal
03/04/2021, 8:13 PMOrhan Tozan
03/04/2021, 8:13 PMSatyam Agarwal
03/04/2021, 8:13 PMOrhan Tozan
03/04/2021, 8:14 PMOrhan Tozan
03/04/2021, 8:14 PMSatyam Agarwal
03/04/2021, 8:15 PMjulian
03/04/2021, 9:39 PMOrhan Tozan
03/05/2021, 12:05 AMjulian
03/05/2021, 12:13 AMjulian
03/05/2021, 12:18 AMsimon.vergauwen
03/05/2021, 6:58 AMThis algorithm takes alot of time to compute, and thus adding suspend is normally recommendedWhere is this being recommended?
suspend is designed to wrap callback-based code. You could use suspend to offload the long-running work on a separate thread, and then finish in a callback-based manner.
@Satyam Agarwal
That is not entirely true. afaik, you would need to returnThis is not the case, there is no need to introduce, if your function has side affects.Either<Throwable, A>
Either<Throwable, A> since suspend already implies Throwable.
Then you have a leak, afaik. Either is specifically designed to contain all side effects.This is actually not the case at all 😅
Either is simply meant to model a branching or a union. It represents a value with one of two possible types.
Whilst suspend models Throwable | A or Result<A>. However, it doens't expose the Throwable on the value level only at a semantic level. Which means you cannot call suspend from normal code but only from other suspend code.
In addition to some other properties which make it the equivalent of IO, for example it being lazy.
It however is a good pratice to type errors, wheter it is Either<Throwable, A> or Either<MyError, A> where MyError is a data class, sealed class or enum class.simon.vergauwen
03/05/2021, 7:06 AMNo, either just says that a function can return two different types, that's all actuallyThis is 100% correct. If your function is impure, wrapping it in
Either will not make it pure. In those cases you want to make it with suspend to make it lazy.simon.vergauwen
03/05/2021, 7:13 AMOrhan Tozan
03/05/2021, 8:11 AMsimon.vergauwen
03/05/2021, 8:18 AMfun algorithm(): Result, which models the concerns correctly.
A caller of this functions can then offload it to an IO thread to prevent it from hogging a computation thread. withContext(<http://Dispatchers.IO|Dispatchers.IO>) { algorithm() } This however makes the caller suspend to call withContext.
If you'd want to enforce your users from calling the algorithm on a background thread, only then would I introduce suspend in the function signature of algorithm.simon.vergauwen
03/05/2021, 8:20 AM