Mark Fisher
02/04/2020, 12:31 PMMark Fisher
02/04/2020, 12:33 PMimport arrow.Kind
import <http://arrow.fx.IO|arrow.fx.IO>
import <http://arrow.fx.extensions.io|arrow.fx.extensions.io>.monadThrow.monadThrow
import arrow.typeclasses.MonadThrow
class MyProcessor {
fun getSomeList(id: String): List<String> {
return listOf("a-$id", "b-$id")
}
}
interface SomeDeps<F>: MonadThrow<F> {
fun processor(): MyProcessor
companion object {
operator fun <F> invoke(ME: MonadThrow<F>, processor: MyProcessor): SomeDeps<F> =
object: SomeDeps<F>, MonadThrow<F> by ME {
override fun processor() = processor
}
}
}
object Api {
fun <F> SomeDeps<F>.getFoo(fid: Kind<F, String>): Kind<F, List<String>> =
fx.monadThrow {
val (id) = fid
processor().getSomeList(id)
}.handleError {
listOf()
}
}
fun main() {
val deps = SomeDeps(IO.monadThrow(), MyProcessor())
// can't do this:
// deps.getFoo(...)
}
I had to add a lot more <F>
types to the definitions than are in the documented example just to compile. Now I'm trying to invoke deps.getFoo()
in main()
but the method isn't found.Mark Fisher
02/04/2020, 12:33 PMaballano
02/04/2020, 12:41 PMgetFoo
is defined as an extension function, you need to create a context for it, in your case
Api.run {
deps.getFoo(...)
}
Mark Fisher
02/04/2020, 12:43 PMMark Fisher
02/04/2020, 1:30 PMwith (Option.monadError()) {
createId("123") shouldBe Some(123)
// ...
but createId
is on monadThrow
not monadError
so this doesn't compile either.
How do I get a monadThrow
from an Option
to make this work?