Created
January 28, 2022 07:40
-
-
Save rssh/5b8986372de66052ddb678b00690c04f to your computer and use it in GitHub Desktop.
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
package cps.examples | |
import scala.concurrent.Await | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration.Duration | |
import scala.util.{Failure, Success} | |
import cps.* // async | |
import cps.monads.{*, given} // toFutureConversion | |
import java.util.concurrent.CompletableFuture | |
object Exceptions: | |
def main(args: Array[String]): Unit = | |
val nArgs = args.length | |
def raiseException(s: String): Unit = | |
val ex = new RuntimeException(s) | |
throw ex | |
val fut0 = async[CompletableFuture]{ | |
val m = summon[CpsMonad[?]] | |
println(s"Run asynchronous task via ${m}") | |
if nArgs > 2 | |
then raiseException(s"$m: $nArgs") | |
else nArgs | |
} | |
val fut = toFutureConversion(fut0) | |
Await.ready(fut, Duration(1, "seconds")) | |
if fut.value.isDefined then | |
fut.value.get match | |
case Failure(ex) => println(ex) | |
case Success(res) => println(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment