Is mixing coroutines with fx discouraged? I'm doin...
# arrow
k
Is mixing coroutines with fx discouraged? I'm doing so in my ktor app (well because ktor uses coroutines :D) would the alternative be to use something other than ktor?
b
k
Ah 😄 No not yet. Looks like exactly what I need.
👍 2
t
Copy code
inline fun <reified T : Any> Route.get(noinline body: suspend ConcurrentSyntax<ForIO>.(call: ApplicationCall, args: T) -> Unit): Route =
    get<T> {
        IO.fx {
            body(call, it)
        }.suspendCancellable()
    }
now Ktor uses IO 🙂
k
Yes that's what I already do. But it looks hacky 😄
t
because of the IO > coroutines boundary crossing?
k
Yeah I guess that's it what feels off about it. https://github.com/kartoffelsup/nuntius/blob/master/adapters/src/main/kotlin/io/github/kartoffelsup/nuntius/Routes.kt#L80-L118 - this is what I'm doing and I mean it works but it feels wrong? Not sure how to explain it 😄
If you wrap your routes on IO you can compose them easier. This is some code with the 0.11 snapshot
k
Yes that looks a lot nicer, thanks! 🙂
r
suspendCancellable is now in Arrow I believe
but that is what it looks like
then you can do things like
those functions will eventually make it to a ktor integration module if they are not already there
The key is to embrace IO / suspend all the way to
main
k
Ah, so this shifts the unsafe part from each route to main?
s
There is nothing dangerous about doing so. And all KotlinX features can still be used 👍
❤️ 1
s
@raulraja I’ve already bumped to 0.10.5, though
suspendCancellable
is there, it doesn’t have the same implementation as in 0.11.0. Anyway, The snippet you posted
<http://embedded.io|embedded.io>.server.kt
in that there is
Copy code
GlobalScope.KtorEmbeddedServer
thats basically
Copy code
GlobalScope.embeddedServer
right ? because
KtorEmbeddedServer
is unresolved, so I am guessing it was just to illustrate ?
And
TaskOf<Unit>
is also unresolved.
s
The key is to embrace IO / suspend all the way to
main
@raulraja are there any examples of how that would look for an android app?
a
@starke yes, one example you have from our colleague @Jorge Castillo https://github.com/JorgeCastilloPrz/ArrowAndroidSamples and there’s even an article posted recently explaining it https://medium.com/default-to-open/explaining-the-arrow-android-sample-ee5c8bdfe88a
s
thank you! I’ll check those out
👍 1