note that `<http://Dispatchers.IO|Dispatchers.IO>`...
# arrow
p
note that
<http://Dispatchers.IO|Dispatchers.IO>
here is where the ios are started, and each can run on its own scheduler
m
I can't get
<http://Dispatchers.IO|Dispatchers.IO>
to work, the Dispatchers it finds is
arrow.fx.typeclasses
, which has an
io()
method, but this doesn't work either. I think i've got a dependency issue.
message has been deleted
What is the Dispatchers package in the example you're using?
I was able to use
IODispatchers.IOPool
s
You can use
IO.dispatchers().default()
or
IO.dispatchers().io()
. You an also parameterize over
Dispatchers
if needed.
<http://Dispatchers.IO|Dispatchers.IO>
Paco is mentioning comes from KotlinX but I don’t advice using those with Arrow Fx since they come with some overhead specific to KotlinX.
😱 1
☝🏼 1
🙈 1
👌🏼 1
m
ah, thanks. I don't have KotlinX on the path, which explains why I'm struggling there. I also noticed there's a
asCoroutineContext()
extension function on Scheduler, so I can use the same pool I was using in the RX world, i.e.
<http://Schedulers.io|Schedulers.io>().asCoroutineContext()
👍🏼 2
btw, @simon.vergauwen I don't understand what "parameterize over Dispatchers" means. Can you give me an example please? I love to be educated 🙂
s
Yes, that absolutely works 1 and might even be more desired to share a pool instead using multiple onces.
With parameterizing over
Dispatchers
I used meant take them as a parameter, so you could for example replace the
CoroutineContext
to use
IO.dispatchers()
or your own
IORxJavaDispatchers
.
Copy code
fun program(dispatchers: Dispatchers<ForIO>) = IO.fx {
   val nameIO = !effect(<http://dispatchers.io|dispatchers.io>()) { println(Thread.currenThread().name) }
   val nameComputation = !effect(dispatchers.computation()) { println(Thread.currenThread().name) }
   !effect { println("$nameIO ~> $nameComputation") }
}

fun <F> rxDispatchers() = object : Dispatchers<F> {
    fun computation() = Schedulers.computation().asCoroutineContext()
    fun io() = <http://Schedulers.io|Schedulers.io>().asCoroutineContext()
}

suspend fun main() {
   program(IO.dispatchers()).suspended()
   program(rxDispatchers<ForIO>()).suspended()
}
👍 1
m
Couple of typos, and method names, but thanks for this example. Helps a lot!
Copy code
import arrow.fx.ForIO
import <http://arrow.fx.IO|arrow.fx.IO>
import arrow.fx.extensions.fx
import <http://arrow.fx.extensions.io|arrow.fx.extensions.io>.dispatchers.dispatchers
import arrow.fx.rx2.extensions.asCoroutineContext
import arrow.fx.typeclasses.Dispatchers
import io.reactivex.schedulers.Schedulers

fun program(dispatchers: Dispatchers<ForIO>) = IO.fx {
    val nameIO = !effect(<http://dispatchers.io|dispatchers.io>()) { println(Thread.currentThread().name) }
    val nameComputation = !effect(dispatchers.default()) { println(Thread.currentThread().name) }
    !effect { println("$nameIO ~> $nameComputation") }
}

fun <F> rxDispatchers() = object : Dispatchers<F> {
    override fun default() = Schedulers.computation().asCoroutineContext()
    override fun io() = <http://Schedulers.io|Schedulers.io>().asCoroutineContext()
}

suspend fun main() {
    program(IO.dispatchers()).suspended()
    program(rxDispatchers()).suspended()
}
s
Yes, sorry about that I wrote it on the top of my head 😄