Joram Visser
06/13/2020, 11:00 PMeffectfulProgram: IO<E, A> I could do effectfulProgram.unsafeRunSync() to run the program from a none suspended context. (For example an endpoint in a framework without coroutines.) Now I am trying out the new Arrow Fx Coroutines lib and transformed my program to effectfulProgram: suspend () Either<E, A>. How do I run it? 😅AdrianRaFo
06/13/2020, 11:48 PMsimon.vergauwen
06/14/2020, 7:24 AMsimon.vergauwen
06/14/2020, 7:29 AMJoram Visser
06/14/2020, 8:02 AMJoram Visser
06/14/2020, 11:01 AMstartCoroutine once more.
fun main(): Unit {
val ctx: CoroutineContext = ComputationPool
suspend { program() }.startCoroutine(Continuation(ctx) { r -> println(r) })
}
This does not run my program. What am I missing? (Or did I find a bug? 🤭)simon.vergauwen
06/14/2020, 11:03 AMunsafeRunSync still exists but it’s internal atm, the difference with runBlocking is that it doesn’t start an EventLoop on the current Thread, but uses a simple Reantrant lock.
https://github.com/arrow-kt/arrow-fx/blob/master/arrow-fx-coroutines/src/main/kotlin/arrow/fx/coroutines/Platform.kt#L128simon.vergauwen
06/14/2020, 11:03 AMsuspend main(): Unit = program()simon.vergauwen
06/14/2020, 11:07 AMOne thing that is less elegant is that I need to wrap the program in an Either.catch (because it might throw an exception), while before it would be implicitly be handled in the IO and I could make use of functions like redeemWith to handle all casesThere is actually no difference between
IO and suspend if you use Arrow Fx Coroutines. Since all it’s operators are lazy like IO, you can install Either.catch at any time in your program just like you can call attempt or redeemWith at any point in time.
So val io2 = io.redeemWith(recover, map) becomes val value = Either.catch { io() }.fold(recover, map).simon.vergauwen
06/14/2020, 11:07 AMEnviroment PR next week and I’ll share it here in the channel 😉Joram Visser
06/14/2020, 11:19 AMJoram Visser
06/14/2020, 12:24 PM