From 5e39bc3669b10a369943d463a9ceea7ed8882060 Mon Sep 17 00:00:00 2001 From: Alejandro Serrano Mena Date: Fri, 23 Aug 2024 17:27:15 +0200 Subject: [PATCH] Make `raise` available in `Option.catch` --- arrow-libs/core/arrow-core/api/arrow-core.api | 8 ++++---- arrow-libs/core/arrow-core/api/arrow-core.klib.api | 4 ++-- .../src/commonMain/kotlin/arrow/core/Option.kt | 11 ++++------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index b7a4f020cf2..60ce585caa5 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -597,15 +597,15 @@ public abstract class arrow/core/Option { public final fun toEither (Lkotlin/jvm/functions/Function0;)Larrow/core/Either; public final fun toList ()Ljava/util/List; public fun toString ()Ljava/lang/String; - public static final fun tryCatch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Larrow/core/Option; - public static final fun tryCatchOrNone (Lkotlin/jvm/functions/Function0;)Larrow/core/Option; + public static final fun tryCatch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/Option; + public static final fun tryCatchOrNone (Lkotlin/jvm/functions/Function1;)Larrow/core/Option; } public final class arrow/core/Option$Companion { public final fun fromNullable (Ljava/lang/Object;)Larrow/core/Option; public final fun invoke (Ljava/lang/Object;)Larrow/core/Option; - public final fun tryCatch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Larrow/core/Option; - public final fun tryCatchOrNone (Lkotlin/jvm/functions/Function0;)Larrow/core/Option; + public final fun tryCatch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/Option; + public final fun tryCatchOrNone (Lkotlin/jvm/functions/Function1;)Larrow/core/Option; } public final class arrow/core/OptionKt { diff --git a/arrow-libs/core/arrow-core/api/arrow-core.klib.api b/arrow-libs/core/arrow-core/api/arrow-core.klib.api index c953876b1ca..406a5a7848e 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.klib.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.klib.api @@ -646,8 +646,8 @@ sealed class <#A: out kotlin/Any?> arrow.core/Option { // arrow.core/Option|null final object Companion { // arrow.core/Option.Companion|null[0] final fun <#A2: kotlin/Any?> fromNullable(#A2?): arrow.core/Option<#A2> // arrow.core/Option.Companion.fromNullable|fromNullable(0:0?){0§}[0] final fun <#A2: kotlin/Any?> invoke(#A2): arrow.core/Option<#A2> // arrow.core/Option.Companion.invoke|invoke(0:0){0§}[0] - final inline fun <#A2: kotlin/Any?> catch(kotlin/Function0<#A2>): arrow.core/Option<#A2> // arrow.core/Option.Companion.catch|catch(kotlin.Function0<0:0>){0§}[0] - final inline fun <#A2: kotlin/Any?> catch(kotlin/Function1>, kotlin/Function0<#A2>): arrow.core/Option<#A2> // arrow.core/Option.Companion.catch|catch(kotlin.Function1>;kotlin.Function0<0:0>){0§}[0] + final inline fun <#A2: kotlin/Any?> catch(kotlin/Function1, #A2>): arrow.core/Option<#A2> // arrow.core/Option.Companion.catch|catch(kotlin.Function1,0:0>){0§}[0] + final inline fun <#A2: kotlin/Any?> catch(kotlin/Function1>, kotlin/Function1, #A2>): arrow.core/Option<#A2> // arrow.core/Option.Companion.catch|catch(kotlin.Function1>;kotlin.Function1,0:0>){0§}[0] } } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt index d445d134165..70b88526a0f 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt @@ -5,6 +5,7 @@ import arrow.core.raise.EagerEffect import arrow.core.raise.Effect import arrow.core.raise.SingletonRaise import arrow.core.raise.option +import arrow.core.raise.recover import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -309,23 +310,19 @@ public sealed class Option { /** * Ignores exceptions and returns None if one is thrown */ - public inline fun catch(f: () -> A): Option { + public inline fun catch(f: SingletonRaise.() -> A): Option { contract { callsInPlace(f, InvocationKind.AT_MOST_ONCE) } return catch({ None }, f) } @JvmStatic @JvmName("tryCatch") - public inline fun catch(recover: (Throwable) -> Option, f: () -> A): Option { + public inline fun catch(recover: (Throwable) -> Option, f: SingletonRaise.() -> A): Option { contract { callsInPlace(f, InvocationKind.AT_MOST_ONCE) callsInPlace(recover, InvocationKind.AT_MOST_ONCE) } - return try { - Some(f()) - } catch (t: Throwable) { - recover(t.nonFatalOrThrow()) - } + return recover({ f(SingletonRaise(this)).some() }, { None }, recover) } }