https://kotlinlang.org logo
#arrow-contributors
Title
# arrow-contributors
p

pakoito

09/28/2021, 3:36 PM
k

kioba

09/29/2021, 11:18 AM
we are planning to do a different
bind
solution on extra requirements in our android state management. We wanted to bring in
Async
work into our scope and also provide an environment for the binding. something similar:
Copy code
public suspend fun <E : Environment, ER, R> EitherScope<E, ER>.bind(
    f: suspend E.() -> Either<R, ER>,
) : R

public suspend fun <E : Environment, ER, R> EitherScope<E, ER>.bindAsync(
    f: suspend E.() -> Either<R, ER>,
) : Deferred<R>
instead of binding on the
Either
type we bind on the scope and the bind argument return value will be evaluated. usage:
Copy code
either {
    val a = bind { this.myNetworkCall() }
    val b = bindAsync { this.myNetworkCall2() }
    b.await()
}
we baked in our Error type but could effectively provide a mapper for as the first argument.
Copy code
bind({ it.message }) { myCall() }
p

pakoito

09/29/2021, 1:48 PM
Isn't that Either.catch? with
.bind
k

kioba

09/29/2021, 3:37 PM
yeah it is 👍
Copy code
val env = getEnvironment()
val a = Either.catch { env.networkCall() }.bind()
our usecase requires the environment so we could add it in scope 🙂
having async also adds quite a lot of nesting so we aimed to reduce the required setup
Copy code
coroutineScope {
    val env = getEnvironment()
    val a = async { Either.catch { env.networkCall() }.bind() }
}
4 Views