Scala/Ammonite FAQ: How do I use ZIO 2 in the Ammonite REPL?
Solution
ZIO can be added into the Ammonite REPL as a managed dependency by using Ammonite’s import $ivy
syntax:
import $ivy.`dev.zio::zio:2.0.22`
However, note that to really use ZIO in the Ammonite REPL, you’ll probably want to use both of these import
statements:
import $ivy.`dev.zio::zio:2.0.22`
import $ivy.`fr.janalyse::zio-worksheet:2.0.12.0`
The first one imports the ZIO library, and the second one imports the ZIO Worksheet library, which lets you easily run ZIO applications in a REPL environment, as shown in their example, which I have converted to the Ammonite REPL syntax:
import $ivy.`dev.zio::zio:2.0.22`
import $ivy.`fr.janalyse::zio-worksheet:2.0.12.0`
import zio.*
import zio.worksheet.*
// create a little application and run it
val app = Console.printLine("Hello world !")
app.unsafeRun
// create another little app and run it
val username: ZIO[Any, Throwable, String] =
ZIO.attempt(scala.io.StdIn.readLine("What’s your name? "))
def printName(name: String): ZIO[Any, Nothing, Unit] =
ZIO.succeed(println(s"Hello, $name"))
val myapp = username.flatMap { name =>
printName(name)
}
myapp.unsafeRun
In summary, if you wanted to see how to use ZIO in the Ammonite REPL, I hope these examples are helpful.
More ZIO information
If you’d like more information on creating ZIO 2 applications, see my free “How to create a small example ZIO 2 application” video.