Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make raise available in Option.catch #3481

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions arrow-libs/core/arrow-core/api/arrow-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -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§<kotlin.Any?>}[0]
final fun <#A2: kotlin/Any?> invoke(#A2): arrow.core/Option<#A2> // arrow.core/Option.Companion.invoke|invoke(0:0){0§<kotlin.Any?>}[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§<kotlin.Any?>}[0]
final inline fun <#A2: kotlin/Any?> catch(kotlin/Function1<kotlin/Throwable, arrow.core/Option<#A2>>, kotlin/Function0<#A2>): arrow.core/Option<#A2> // arrow.core/Option.Companion.catch|catch(kotlin.Function1<kotlin.Throwable,arrow.core.Option<0:0>>;kotlin.Function0<0:0>){0§<kotlin.Any?>}[0]
final inline fun <#A2: kotlin/Any?> catch(kotlin/Function1<arrow.core.raise/SingletonRaise<arrow.core/None>, #A2>): arrow.core/Option<#A2> // arrow.core/Option.Companion.catch|catch(kotlin.Function1<arrow.core.raise.SingletonRaise<arrow.core.None>,0:0>){0§<kotlin.Any?>}[0]
final inline fun <#A2: kotlin/Any?> catch(kotlin/Function1<kotlin/Throwable, arrow.core/Option<#A2>>, kotlin/Function1<arrow.core.raise/SingletonRaise<arrow.core/None>, #A2>): arrow.core/Option<#A2> // arrow.core/Option.Companion.catch|catch(kotlin.Function1<kotlin.Throwable,arrow.core.Option<0:0>>;kotlin.Function1<arrow.core.raise.SingletonRaise<arrow.core.None>,0:0>){0§<kotlin.Any?>}[0]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -309,23 +310,19 @@ public sealed class Option<out A> {
/**
* Ignores exceptions and returns None if one is thrown
*/
public inline fun <A> catch(f: () -> A): Option<A> {
public inline fun <A> catch(f: SingletonRaise<None>.() -> A): Option<A> {
contract { callsInPlace(f, InvocationKind.AT_MOST_ONCE) }
return catch({ None }, f)
}

@JvmStatic
@JvmName("tryCatch")
public inline fun <A> catch(recover: (Throwable) -> Option<A>, f: () -> A): Option<A> {
public inline fun <A> catch(recover: (Throwable) -> Option<A>, f: SingletonRaise<None>.() -> A): Option<A> {
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)
}
}

Expand Down