Forgive me, I’m coming back to android after some ...
# arrow
r
Forgive me, I’m coming back to android after some time away. I’ve updated to the latest stable version of Arrow (0.13.2) and I’m trying to use monad comprehensions. I’m running into an issue where
bind
is not defined. I’m currently using core, optics, and meta (via kapt). What am I doing wrong?
s
Hey @Robert Menke, It looks like you don’t have the arrow import. The receiver type you have there
OptionKt.DSL
is not from Arrow. You want to use
arrow.core.computations.option
, and in the case where you want to assign to a
val
.
Copy code
val contents = option.eager { ... }
r
You are a true hero @simon.vergauwen thanks for the direction! I’m not sure I understand why I need to use
option.eager
though.
Copy code
@Suppress("ClassName")
object option {
  inline fun <A> eager(crossinline func: suspend RestrictedOptionEffect<A>.() -> A): Option<A> =
    Effect.restricted(eff = { RestrictedOptionEffect { it } }, f = func, just = { Option.fromNullable(it) })

  suspend inline operator fun <A> invoke(crossinline func: suspend OptionEffect<*>.() -> A?): Option<A> =
    Effect.suspended(eff = { OptionEffect { it } }, f = func, just = { Option.fromNullable(it) })
}
It seems that the only difference between the operator function and eager is the use of
RestrictedOptionEffect
, but they both return
Option<A>
.
s
You’re very welcome! The DSL uses the suspension system, but the Restricted version uses
@RestrictSuspension
. This disallows any suspend function to be called inside the DSL except for the functions defined inside
RestrictOptionEffect
. This means that the DSL can be evaluated immediately, or `eager`ly. Instead of requiring
suspend
. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/-restricts-suspension/
r
Oh got it, I was already in a suspend context in this case so would
option
be preferred to
option.eager
here?
s
If you’re already inside a suspend context then it makes most sense to just use the suspend version yes. That’ll also allow you to use other suspending code inside the DSL
Btw there is also nullable as a DSL if you’re working with nullable already
r
Awesome, any way I can tip you for your time/effort? Seriously blown away by the generosity of this community every time I’m back on an android project.
s
‘nullable’
r
Oooo
nullable
DSL sounds excellent
s
Not really :) Thanks for the offer though
Happy to help
r
You rock ❤️