-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into renovate/all
- Loading branch information
Showing
6 changed files
with
74 additions
and
11 deletions.
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
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
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
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
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
56 changes: 56 additions & 0 deletions
56
arrow-libs/core/arrow-core/src/jvmTest/kotlin/arrow/core/continuations/EffectUsage.kt
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package arrow.core.continuations | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
sealed interface MyError | ||
sealed interface SubError : MyError | ||
sealed interface OtherError : MyError { | ||
object Actual : OtherError | ||
} | ||
|
||
context(EffectScope<SubError>) | ||
suspend fun subprogram(): Unit = | ||
println("Hello SubProgram!") | ||
|
||
context(EffectScope<OtherError>) | ||
suspend fun otherprogram(): Unit = | ||
println("Hello OtherProgram!") | ||
|
||
context(EffectScope<OtherError>) | ||
suspend fun fail(): MyResponse = | ||
shift(OtherError.Actual) | ||
|
||
fun main() = | ||
runBlocking(Dispatchers.Default) { | ||
effect<MyError, Unit> { | ||
subprogram() | ||
otherprogram() | ||
fail() | ||
}.fold(::println) { } | ||
// Hello SubProgram! | ||
// Hello OtherProgram! | ||
// OtherError$Actual@7cd62f43 | ||
} | ||
|
||
sealed interface MyResponse | ||
object EmptyResponse : MyResponse | ||
data class ErrorResponse(val error: Throwable) : MyResponse | ||
data class BodyResponse(val body: String) : MyResponse | ||
|
||
context(EffectScope<SubError>) | ||
suspend fun respondWithBody(): BodyResponse = | ||
BodyResponse("Hello Program!") | ||
|
||
context(EffectScope<OtherError>) | ||
suspend fun attemptOrError(): MyResponse = | ||
ErrorResponse(RuntimeException("Oh no!")) | ||
|
||
fun respond(): Effect<MyError, MyResponse> = | ||
effect { | ||
when (attemptOrError()) { | ||
is BodyResponse -> respondWithBody() | ||
EmptyResponse -> EmptyResponse | ||
is ErrorResponse -> fail() | ||
} | ||
} |