functionality and I try to find a library function similar to the following hypothetical
orElse()
:
Copy code
context(Raise<String>)
fun f1() = 1
context(Raise<String>)
fun f2(): Nothing = raise("error")
suspend inline fun <R, A> Effect<R, A>.orElse(noinline block: suspend (R) -> A) =
fold(
recover = block,
transform = ::identity
)
val v1 = effect { f1() }.orElse { 33 }
assertEquals(1, v1)
val v2 = effect { f2() }.orElse { 33 }
assertEquals(33, v2)
I use arrow 1.1.6-alpha.36.
Thanks.
s
simon.vergauwen
03/01/2023, 9:38 AM
Such an API doesn't exist yet 🤔 but would be welcomed!
I think the signature should probably be
getOrElse(onRaise: (R) -> A): A
. To be in line with other operators and Kotlin Std.
A PR would be awesome, or a issue 🙏
simon.vergauwen
03/01/2023, 9:39 AM
You can do
either { f1() }.getOrElse { 33 }
though.
simon.vergauwen
03/01/2023, 9:40 AM
That would work in your case here without ever even needing
suspend
n
Norbi
03/01/2023, 9:43 AM
without ever even needing
suspend
Really, thanks.
(TBH I'm a little bit confused when I look at the sources because Idea often takes me to the wrong source codes. When I start at browsing
Raise<R>
I often end up at functions related to
arrow.core.continuations.Effect
for some reason 😕 )
s
simon.vergauwen
03/01/2023, 9:56 AM
That is really strange.. I have some issues with browsing code in IDEA. I can almost never see code from dependencies 😕 I know some issues have been reported about this but not sure where it stands.