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

Favor Either and IO instead of Try #1478

Merged
merged 7 commits into from Jun 9, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions modules/core/arrow-core-data/src/main/kotlin/arrow/core/Either.kt
Expand Up @@ -189,6 +189,16 @@ sealed class Either<out A, out B> : EitherOf<A, B> {
}

fun <L, R> cond(test: Boolean, ifTrue: () -> R, ifFalse: () -> L): Either<L, R> = if (test) right(ifTrue()) else left(ifFalse())

suspend fun <R> catch(f: suspend () -> R): Either<Throwable, R> =
catch(::identity, f)

suspend fun <L, R> catch(fe: (Throwable) -> L, f: suspend () -> R): Either<L, R> =
try {
f().right()
} catch (t: Throwable) {
fe(t.nonFatalOrThrow()).left()
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions modules/core/arrow-core-data/src/main/kotlin/arrow/core/Try.kt
Expand Up @@ -16,8 +16,16 @@ sealed class Try<out A> : TryOf<A> {

companion object {

@Deprecated(
"Try promotes eager execution of effects, so it's better if you work with suspend Either constructors or a an effect handler like IO",
ReplaceWith("Either.just(a)")
)
fun <A> just(a: A): Try<A> = Success(a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor is fine if we use suspend for Try's effects


@Deprecated(
"Try promotes eager execution of effects, so it's better if you work with suspend Either constructors or a an effect handler like IO",
ReplaceWith("Either.tailRecM(a, f)")
)
tailrec fun <A, B> tailRecM(a: A, f: (A) -> TryOf<Either<A, B>>): Try<B> {
val ev: Try<Either<A, B>> = f(a).fix()
return when (ev) {
Expand All @@ -32,6 +40,10 @@ sealed class Try<out A> : TryOf<A> {
}
}

@Deprecated(
"Try promotes eager execution of effects, so it's better if you work with suspend Either constructors or a an effect handler like IO",
ReplaceWith("Either.catch(f)")
)
inline operator fun <A> invoke(f: () -> A): Try<A> =
try {
Success(f())
Expand All @@ -43,6 +55,10 @@ sealed class Try<out A> : TryOf<A> {
}
}

@Deprecated(
"Try promotes eager execution of effects, so it's better if you work with suspend Either constructors or a an effect handler like IO",
ReplaceWith("Either.raiseError(e)")
)
fun raiseError(e: Throwable): Try<Nothing> = Failure(e)
}

Expand Down