Hi Arrow people, I've got another question on docs...
# arrow
m
Hi Arrow people, I've got another question on docs examples. I can't get the "Using DI to inject any object" examples compiling, and I'm getting stuck on them. The docs are here https://arrow-kt.io/docs/patterns/dependency_injection/ my code snippet to attempt similar is in thread...
Copy code
import 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.
Can you tell me what I'm doing wrong?
a
since
getFoo
is defined as an extension function, you need to create a context for it, in your case
Copy code
Api.run {
  deps.getFoo(...)
}
m
aha, thanks
Another question, I can't get the lower part to work either, in the docs it has:
Copy code
with (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?