diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index 9d6ddfba8c1..fc33b4f3d0d 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -2631,21 +2631,25 @@ public final class arrow/core/continuations/EagerEffectKt { } public abstract interface class arrow/core/continuations/EagerEffectScope { + public abstract fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public abstract fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } public final class arrow/core/continuations/EagerEffectScope$DefaultImpls { + public static fun attempt (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EagerEffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EagerEffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EagerEffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static fun ensure (Larrow/core/continuations/EagerEffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } @@ -2688,23 +2692,27 @@ public final class arrow/core/continuations/EffectKt { } public abstract interface class arrow/core/continuations/EffectScope { + public abstract fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } public final class arrow/core/continuations/EffectScope$DefaultImpls { + public static fun attempt (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun ensure (Larrow/core/continuations/EffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } @@ -2720,12 +2728,14 @@ public final class arrow/core/continuations/FoldContinuation : kotlin/coroutines public final class arrow/core/continuations/IorEagerEffectScope : arrow/core/continuations/EagerEffectScope, arrow/typeclasses/Semigroup { public fun (Larrow/typeclasses/Semigroup;Larrow/core/continuations/EagerEffectScope;)V + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun bind (Larrow/core/Ior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public fun combine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLeftState ()Ljava/util/concurrent/atomic/AtomicReference; @@ -2737,6 +2747,7 @@ public final class arrow/core/continuations/IorEagerEffectScope : arrow/core/con public final class arrow/core/continuations/IorEffectScope : arrow/core/continuations/EffectScope, arrow/typeclasses/Semigroup { public fun (Larrow/typeclasses/Semigroup;Larrow/core/continuations/EffectScope;)V + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun bind (Larrow/core/Ior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2744,6 +2755,7 @@ public final class arrow/core/continuations/IorEffectScope : arrow/core/continua public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun combine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLeftState ()Ljava/util/concurrent/atomic/AtomicReference; @@ -2754,6 +2766,8 @@ public final class arrow/core/continuations/IorEffectScope : arrow/core/continua } public final class arrow/core/continuations/NullableEagerEffectScope : arrow/core/continuations/EagerEffectScope { + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2767,6 +2781,8 @@ public final class arrow/core/continuations/NullableEagerEffectScope : arrow/cor public static final fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/NullableEagerEffectScope; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/EagerEffectScope; public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun ensure-impl (Larrow/core/continuations/EagerEffectScope;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2785,6 +2801,8 @@ public final class arrow/core/continuations/NullableEagerEffectScope : arrow/cor } public final class arrow/core/continuations/NullableEffectScope : arrow/core/continuations/EffectScope { + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2800,6 +2818,8 @@ public final class arrow/core/continuations/NullableEffectScope : arrow/core/con public static final fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/NullableEffectScope; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/EffectScope; public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun ensure-impl (Larrow/core/continuations/EffectScope;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2823,6 +2843,8 @@ public final class arrow/core/continuations/NullableKt { } public final class arrow/core/continuations/OptionEagerEffectScope : arrow/core/continuations/EagerEffectScope { + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2835,6 +2857,8 @@ public final class arrow/core/continuations/OptionEagerEffectScope : arrow/core/ public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/OptionEagerEffectScope; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/EagerEffectScope; public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun ensure-impl (Larrow/core/continuations/EagerEffectScope;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2853,6 +2877,8 @@ public final class arrow/core/continuations/OptionEagerEffectScope : arrow/core/ } public final class arrow/core/continuations/OptionEffectScope : arrow/core/continuations/EffectScope { + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2867,6 +2893,8 @@ public final class arrow/core/continuations/OptionEffectScope : arrow/core/conti public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/OptionEffectScope; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/EffectScope; public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun ensure-impl (Larrow/core/continuations/EffectScope;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2892,6 +2920,8 @@ public final class arrow/core/continuations/OptionKt { } public final class arrow/core/continuations/ResultEagerEffectScope : arrow/core/continuations/EagerEffectScope { + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2904,6 +2934,8 @@ public final class arrow/core/continuations/ResultEagerEffectScope : arrow/core/ public static final fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/ResultEagerEffectScope; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/EagerEffectScope; public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun ensure-impl (Larrow/core/continuations/EagerEffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2921,6 +2953,8 @@ public final class arrow/core/continuations/ResultEagerEffectScope : arrow/core/ } public final class arrow/core/continuations/ResultEffectScope : arrow/core/continuations/EffectScope { + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2935,6 +2969,8 @@ public final class arrow/core/continuations/ResultEffectScope : arrow/core/conti public static final fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/ResultEffectScope; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/EffectScope; public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun ensure-impl (Larrow/core/continuations/EffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffectScope.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffectScope.kt index a0be9e206bf..2eedc8bf645 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffectScope.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffectScope.kt @@ -10,6 +10,8 @@ import arrow.core.identity import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract import kotlin.coroutines.RestrictsSuspension +import kotlin.experimental.ExperimentalTypeInference +import kotlin.jvm.JvmInline /** Context of the [EagerEffect] DSL. */ @RestrictsSuspension @@ -186,6 +188,56 @@ public interface EagerEffectScope { */ public suspend fun ensure(condition: Boolean, shift: () -> R): Unit = if (condition) Unit else shift(shift()) + + /** + * Encloses an action for which you want to catch any `shift`. + * [attempt] is used in combination with [catch]. + * + * ``` + * attempt { ... } catch { ... } + * ``` + * + * The [f] may `shift` into a different `EagerEffectScope`, giving + * the chance for a later [catch] to change the shifted value. + * This is useful to simulate re-throwing of exceptions. + */ + @OptIn(ExperimentalTypeInference::class) + public suspend fun attempt( + @BuilderInference + f: suspend EagerEffectScope.() -> A, + ): suspend EagerEffectScope.() -> A = f + + + /** + * When the [EagerEffect] has shifted with [R] it will [recover] + * the shifted value to [A], and when it ran the computation to + * completion it will return the value [A]. + * [catch] is used in combination with [attempt]. + * + * ```kotlin + * import arrow.core.Either + * import arrow.core.None + * import arrow.core.Option + * import arrow.core.Validated + * import arrow.core.continuations.eagerEffect + * import io.kotest.assertions.fail + * import io.kotest.matchers.shouldBe + * + * fun main() { + * eagerEffect { + * val x = Either.Right(1).bind() + * val y = Validated.Valid(2).bind() + * val z = + * attempt { None.bind { "Option was empty" } } catch { 0 } + * x + y + z + * }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 }) + * } + * ``` + * + */ + public infix fun (suspend EagerEffectScope.() -> A).catch( + recover: EagerEffectScope.(E) -> A, + ): A = eagerEffect(this).fold({ recover(it) }, ::identity) } /** @@ -207,10 +259,11 @@ public interface EagerEffectScope { * }.toEither() shouldBe (int?.right() ?: failure.left()) * } * ``` - * + * */ @OptIn(ExperimentalContracts::class) public suspend fun EagerEffectScope.ensureNotNull(value: B?, shift: () -> R): B { contract { returns() implies (value != null) } return value ?: shift(shift()) } + diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EffectScope.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EffectScope.kt index 60e9332711d..b566dbeec8c 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EffectScope.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EffectScope.kt @@ -9,6 +9,8 @@ import arrow.core.Validated import arrow.core.identity import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract +import kotlin.experimental.ExperimentalTypeInference +import kotlin.jvm.JvmInline /** Context of the [Effect] DSL. */ public interface EffectScope { @@ -215,6 +217,55 @@ public interface EffectScope { */ public suspend fun ensure(condition: Boolean, shift: () -> R): Unit = if (condition) Unit else shift(shift()) + + /** + * Encloses an action for which you want to catch any `shift`. + * [attempt] is used in combination with [catch]. + * + * ``` + * attempt { ... } catch { ... } + * ``` + * + * The [f] may `shift` into a different `EffectScope`, giving + * the chance for a later [catch] to change the shifted value. + * This is useful to simulate re-throwing of exceptions. + */ + @OptIn(ExperimentalTypeInference::class) + public suspend fun attempt( + @BuilderInference + f: suspend EffectScope.() -> A, + ): suspend EffectScope.() -> A = f + + /** + * When the [Effect] has shifted with [R] it will [recover] + * the shifted value to [A], and when it ran the computation to + * completion it will return the value [A]. + * [catch] is used in combination with [attempt]. + * + * ```kotlin + * import arrow.core.Either + * import arrow.core.None + * import arrow.core.Option + * import arrow.core.Validated + * import arrow.core.continuations.effect + * import io.kotest.assertions.fail + * import io.kotest.matchers.shouldBe + * + * suspend fun main() { + * effect { + * val x = Either.Right(1).bind() + * val y = Validated.Valid(2).bind() + * val z = + * attempt { None.bind { "Option was empty" } } catch { 0 } + * x + y + z + * }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 }) + * } + * ``` + * + */ + public suspend infix fun (suspend EffectScope.() -> A).catch( + recover: suspend EffectScope.(E) -> A, + ): A = effect(this).fold({ recover(it) }, ::identity) } /** @@ -236,10 +287,11 @@ public interface EffectScope { * }.toEither() shouldBe (int?.right() ?: failure.left()) * } * ``` - * + * */ @OptIn(ExperimentalContracts::class) public suspend fun EffectScope.ensureNotNull(value: B?, shift: () -> R): B { contract { returns() implies (value != null) } return value ?: shift(shift()) } + diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt index 040657f0991..7c32aeae4c4 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt @@ -10,6 +10,7 @@ import io.kotest.matchers.shouldBe import io.kotest.property.Arb import io.kotest.property.arbitrary.boolean import io.kotest.property.arbitrary.int +import io.kotest.property.arbitrary.long import io.kotest.property.arbitrary.orNull import io.kotest.property.arbitrary.string import io.kotest.property.checkAll @@ -57,6 +58,32 @@ class EagerEffectSpec : StringSpec({ } } + "attempt - catch" { + checkAll(Arb.int(), Arb.long()) { i, l -> + eagerEffect { + attempt { + shift(l) + } catch { ll -> + ll shouldBe l + i + } + }.runCont() shouldBe i + } + } + + "attempt - no catch" { + checkAll(Arb.int(), Arb.long()) { i, l -> + eagerEffect { + attempt { + i + } catch { ll -> + ll shouldBe l + i + 1 + } + }.runCont() shouldBe i + } + } + "immediate values" { eagerEffect { 1 }.toEither().orNull() shouldBe 1 } "immediate short-circuit" { eagerEffect { shift("hello") }.runCont() shouldBe "hello" } diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt index 4bc582ee6e2..7fe46a309a6 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt @@ -10,6 +10,7 @@ import io.kotest.matchers.shouldBe import io.kotest.property.Arb import io.kotest.property.arbitrary.boolean import io.kotest.property.arbitrary.int +import io.kotest.property.arbitrary.long import io.kotest.property.arbitrary.orNull import io.kotest.property.arbitrary.string import io.kotest.property.checkAll @@ -109,6 +110,32 @@ class EffectSpec : } } + "attempt - catch" { + checkAll(Arb.int(), Arb.long()) { i, l -> + effect { + attempt { + shift(l) + } catch { ll -> + ll shouldBe l + i + } + }.runCont() shouldBe i + } + } + + "attempt - no catch" { + checkAll(Arb.int(), Arb.long()) { i, l -> + effect { + attempt { + i + } catch { ll -> + ll shouldBe l + i + 1 + } + }.runCont() shouldBe i + } + } + "eagerEffect can be consumed within an Effect computation" { checkAll(Arb.int(), Arb.int()) { a, b -> val eager: EagerEffect = diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-08.kt index 4f62bcbc504..a68c64d66ca 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-08.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-08.kt @@ -1,16 +1,20 @@ // This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEagerEffectScope08 +import arrow.core.Either +import arrow.core.None +import arrow.core.Option +import arrow.core.Validated import arrow.core.continuations.eagerEffect -import arrow.core.continuations.ensureNotNull -import arrow.core.left -import arrow.core.right +import io.kotest.assertions.fail import io.kotest.matchers.shouldBe fun main() { - val failure = "failed" - val int: Int? = null eagerEffect { - ensureNotNull(int) { failure } - }.toEither() shouldBe (int?.right() ?: failure.left()) + val x = Either.Right(1).bind() + val y = Validated.Valid(2).bind() + val z = + attempt { None.bind { "Option was empty" } } catch { 0 } + x + y + z + }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 }) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-09.kt new file mode 100644 index 00000000000..9bd00d04b02 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-09.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEagerEffectScope09 + +import arrow.core.continuations.eagerEffect +import arrow.core.continuations.ensureNotNull +import arrow.core.left +import arrow.core.right +import io.kotest.matchers.shouldBe + +fun main() { + val failure = "failed" + val int: Int? = null + eagerEffect { + ensureNotNull(int) { failure } + }.toEither() shouldBe (int?.right() ?: failure.left()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-09.kt index dc5178b8589..4b4ffa3eb7b 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-09.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-09.kt @@ -1,16 +1,20 @@ // This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEffectScope09 +import arrow.core.Either +import arrow.core.None +import arrow.core.Option +import arrow.core.Validated import arrow.core.continuations.effect -import arrow.core.continuations.ensureNotNull -import arrow.core.left -import arrow.core.right +import io.kotest.assertions.fail import io.kotest.matchers.shouldBe suspend fun main() { - val failure = "failed" - val int: Int? = null effect { - ensureNotNull(int) { failure } - }.toEither() shouldBe (int?.right() ?: failure.left()) + val x = Either.Right(1).bind() + val y = Validated.Valid(2).bind() + val z = + attempt { None.bind { "Option was empty" } } catch { 0 } + x + y + z + }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 }) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-10.kt new file mode 100644 index 00000000000..0d0dc84b89e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-10.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffectScope10 + +import arrow.core.continuations.effect +import arrow.core.continuations.ensureNotNull +import arrow.core.left +import arrow.core.right +import io.kotest.matchers.shouldBe + +suspend fun main() { + val failure = "failed" + val int: Int? = null + effect { + ensureNotNull(int) { failure } + }.toEither() shouldBe (int?.right() ?: failure.left()) +}