arrow-kt / arrow

Λrrow - Functional companion to Kotlin's Standard Library
http://arrow-kt.io
Other
6.13k stars 442 forks source link

["Request"] `OptionRaise` available in `Option.catch {}` #3461

Open postfixNotation opened 2 months ago

postfixNotation commented 2 months ago

I'm often using Option.catch {} but I also need the option {} computation block to have access to OptionRaise. Couldn't the lambda of the former be an OptionRaise receiver to get rid of flatten and one level of indentation?

context(OptionRaise)
fun <T> dataOrThrow(): Option<T> {
   ...
}

This is how I have to do it now:

fun <T> foo() {
   val dataOption: Option<T> = Option.catch {
      option {
         dataOrThrow()
      }
   }.flatten()
}

And this is how I want it to be:

fun <T> foo() {
   val dataOption: Option<T> = Option.catch {
      dataOrThrow()
   }
}

So basically I need an option computation block which catches thrown exceptions and translates them as None.