https://kotlinlang.org logo
#arrow
Title
# arrow
d

dave08

03/13/2023, 4:27 PM
If I have:
Copy code
fun foo() = effect {
  raise(DomainError)
}

fun bar() = effect {
  val context = ....
  val result = foo().bind()
}

data class WrappedError(val context: Context, val error: DomainError)

// elsewhere
bar().fold({ e -> // I need e to be WrappedError but I can only get the context in bar() ... and foo() raises DomainErrors w/o the context. }) { ... }
s

simon.vergauwen

03/13/2023, 4:31 PM
So
bar
should return
Effect<WrappedError, ?>
? You can use
recover
inside of
effect { }
(or as top-level anywhere else).
Copy code
fun bar() = effect {
  val context = ...
  val result = recover({
    foo().bind()
  }) { e: DomainError -> raise(WrappedError(context, e)) }
}
d

dave08

03/13/2023, 4:34 PM
So I'd need to surround the whole inside of the effect block with recover...? I wonder if there's a variant of effect { } that allows that for the whole block?
s

simon.vergauwen

03/13/2023, 4:35 PM
Well, if you have access to
context
outside of
effect { }
you can also do
foo().recover { e: DomainError -> raise(WrappedError(context, e)) }
d

dave08

03/13/2023, 4:35 PM
Yeah, that's the little problem here...
🙃 I guess I'll have to survive one extra level of nesting... thanks!
s

simon.vergauwen

03/13/2023, 4:42 PM
Nesting is the price we pay for Kotlin DSLs. I personally don't mind it. you can always hide it in an additional function. So it's not the same as callback hell.
IMO: nesting DSLs is Kotlin's super power 😅 If you compare it to other patterns in other languages.
d

dave08

03/13/2023, 4:46 PM
Yeah, true. But in certain cases it improves readability, and in others reduces it... I guess it all depends where. But it's a good point that in those cases, it can be hidden in a function... I'm still getting used to "FP"s "create a function to avoid imperative programming" 😊
s

simon.vergauwen

03/13/2023, 4:49 PM
Not sure I understand that 🤔 This is the same in all of Kotlin, no? It's similar to
with
,
apply
,
?.let
,
withContext
,
withTimeout
, etc. Unrelated to FP, or OOP.
d

dave08

03/13/2023, 4:55 PM
In this particular function, some of the nesting is not as general a function as
with
or
apply
, it won't necessarily be reused, the function just speaks the domain language better (and at the same time avoids some of the nesting...). I just didn't bother separating it until I got to this nesting.
7 Views