- Notifications
You must be signed in to change notification settings - Fork 151
Description
As soon as a script have to deal with an execution context, or asynchronous processing such as scala Future, ZIO, cats, ...) it is needed to encapsulate the code within an object in order for the script to execute properly, if not it blocks/timeouts without giving any results.
Ammonite has the same issue, see : com-lihaoyi/Ammonite#802 and I was able in some cases to mitigate the issue by adding an ammonite startup option : amm --class-based ....
I think this is an important issue, as it makes our scripts more verbose by systematically having to encapsulate part of the code within objects, and requiring us then to call a function within that object to start the execution.
See next examples, but if required I can provide more examples as the issue is quite easy to reproduce as soon as you use libraries such as AKKA, ZIO, CATS, DOODLE, ...
I was using scala-cli 0.0.9 in this test and scala3, but it is reproductible with any other scala releases.
KO example : (https://gist.github.com/dacr/47ca242e6d672e0154c5816a0330eea7)
// --------------------- // using scala 3.1.0 // --------------------- import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor, Future} import scala.concurrent.duration.* implicit val ec: ExecutionContextExecutor = ExecutionContext.global val future = for message <- Future("Hello world") _ <- Future(println(message)) yield () Await.ready(future, 5.second) OK example : (https://gist.github.com/dacr/13ff195adf279798ed5dcc16da2a33ba)
// --------------------- // using scala 3.1.0 // --------------------- import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor, Future} import scala.concurrent.duration.* object Encapsulated: implicit val ec: ExecutionContextExecutor = ExecutionContext.global def run()= val future = for message <- Future("Hello world") _ <- Future(println(message)) yield () Await.ready(future, 5.second) Encapsulated.run() Executions results :
$ scala-cli https://gist.github.com/dacr/13ff195adf279798ed5dcc16da2a33ba Compiling project (Scala 3.1.0, JVM) Compiled project (Scala 3.1.0, JVM) Hello world $ scala-cli https://gist.github.com/dacr/47ca242e6d672e0154c5816a0330eea7 ... Caused by: java.util.concurrent.TimeoutException: Future timed out after [5 seconds] at scala.concurrent.impl.Promise$DefaultPromise.tryAwait0(Promise.scala:212) at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:219) at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:104) at scala.concurrent.Await$.$anonfun$ready$1(package.scala:174) at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:62) at scala.concurrent.Await$.ready(package.scala:124) at example$minusko$.<clinit>(47ca242e6d672e0154c5816a0330eea7-master/example-ko.sc:26)Metadata
Metadata
Assignees
Labels
Type
Projects
Status