Hello everyone, I am learning how to use typed err...
# arrow
i
Hello everyone, I am learning how to use typed errors and the
Raise
DSL. I am having trouble importing the
bind()
method: IntelliJ keeps telling me that it's an unresolved reference. I have invalidated the IDE's cache and restarted a few times now, but it does not work. Below is my code:
Copy code
import arrow.core.Either
import arrow.core.raise.*
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response

fun getResponseNew(url: String): Either<Error, Response> = either {
    val client = OkHttpClient()
    val request = Request.Builder().url(url).build()
    client.newCall(request).execute().use {response ->
        ensure(response.code == 200) { Error.ResponseCodeNot200 }
        response
    }
}

sealed class Error {
    data object ResponseCodeNot200 : Error()
}
Now if I were to type the following, that's where I'd see the unresolved reference notification:
Copy code
fun main() {
    val myResponse = getResponseNew(<url-string-for-the-endpoint>)
    println(myResponse.bind().code) // To see the status code
}
What should I do to resolve this? Thanks. Also, part of my `build.gradle.kts`:
Copy code
dependencies {
    implementation("io.arrow-kt:arrow-core:1.2.4")
    implementation("io.arrow-kt:arrow-fx-coroutines:1.2.4")
    implementation("com.squareup.okhttp3:okhttp:4.12.0")
    implementation("com.google.code.gson:gson:2.11.0")
}
This is what IntelliJ is showing me:
d
bind
is not what you want here. If you want to access the response you should
getOrElse
.
i
Thanks a lot David! Let me look at the docs you shared
r
You can only use
bind
inside a scope of
Raise<E>.() -> A
functions and builders like
either
provide the scope where bind is resolvable and can be invoked. For example:
Copy code
val maybeTwo: Either<Problem, Int> = either { 2 }
val maybeFive: Either<Problem, Int> = either { raise(Problem) }

val maybeSeven: Either<Problem, Int> = either {
  maybeTwo.bind() + maybeFive.bind()
}
Here
bind
is available as extension to
maybeTwo
and
maybeFive
because the
either
block provides a
Raise<E>.() -> A
scope in its block. In your example
bind
is not resolved because
main
has no scope for Raise.
👍 1