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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c37e70
Add catch constructor to Either
f9dddf8
Add other catch constructor to Either
25bef35
Add deprecation notices in favor of Either
0f62b53
Fix ReplaceWith
a25bd44
Merge branch 'master' into paco-catcheither
pakoito b6e79b7
Make Either constructors suspend
09c71d2
Improve Try deprecation notice
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
|
||
fun <R> catch(f: () -> R): Either<Throwable, R> = | ||
catch(::identity, f) | ||
|
||
fun <L, R> catch(fe: (Throwable) -> L, f: () -> R): Either<L, R> = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suspend |
||
try { | ||
f().right() | ||
} catch (t: Throwable) { | ||
fe(t.nonFatalOrThrow()).left() | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,16 @@ sealed class Try<out A> : TryOf<A> { | |
|
||
companion object { | ||
|
||
@Deprecated( | ||
"Try promotes eager execution and works like Either, so it's better if you work with Either or a suspended type like IO", | ||
ReplaceWith("Either.just(a)") | ||
) | ||
fun <A> just(a: A): Try<A> = Success(a) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constructor is fine if we use suspend for |
||
|
||
@Deprecated( | ||
"Try promotes eager execution and works like Either, so it's better if you work with Either or a suspended type 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) { | ||
|
@@ -32,6 +40,10 @@ sealed class Try<out A> : TryOf<A> { | |
} | ||
} | ||
|
||
@Deprecated( | ||
"Try promotes eager execution and works like Either, so it's better if you work with Either or a suspended type like IO", | ||
ReplaceWith("Either.catch(f)") | ||
) | ||
inline operator fun <A> invoke(f: () -> A): Try<A> = | ||
try { | ||
Success(f()) | ||
|
@@ -43,6 +55,10 @@ sealed class Try<out A> : TryOf<A> { | |
} | ||
} | ||
|
||
@Deprecated( | ||
"Try promotes eager execution and works like Either, so it's better if you work with Either or a suspended type like IO", | ||
ReplaceWith("Either.raiseError(e)") | ||
) | ||
fun raiseError(e: Throwable): Try<Nothing> = Failure(e) | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
suspend fun <R> catch...