Ian Koh
08/20/2024, 9:26 AMRaise
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:
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:
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`:
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")
}
Ian Koh
08/20/2024, 9:30 AMDavid Kubecka
08/20/2024, 9:35 AMbind
is not what you want here. If you want to access the response you should getOrElse
.David Kubecka
08/20/2024, 9:37 AMIan Koh
08/20/2024, 9:41 AMraulraja
08/20/2024, 10:15 AMbind
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:
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.