Skip to content

Instantly share code, notes, and snippets.

@rssh
Created January 28, 2022 07:40
Show Gist options
  • Save rssh/5b8986372de66052ddb678b00690c04f to your computer and use it in GitHub Desktop.
Save rssh/5b8986372de66052ddb678b00690c04f to your computer and use it in GitHub Desktop.
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