Saltar al contenido principal

Scala CLI

Dependencies

Dependencies and compiler options need to be defined before you start the REPL session.

Option 1: define dependencies when starting the REPL session

$ scala-cli repl --dep org.typelevel::cats-core:2.12.0 --dep org.typelevel::cats-effect:3.5.4

scala> import cats.syntax.all.*

scala> val someInt: Option[Int] = Some(3)
val someInt: Option[Int] = Some(3)

scala> val someLong: Option[Long] = Some(4L)
val someLong: Option[Long] = Some(4)

scala> someInt. map2(someLong)((i, l) => i. toString + l. toString)
val res0: Option[String] = Some(34)

Option 2: define dependencies in a file

The dependencies are defined in the file dep.scala:

//> using dep org.typelevel::cats-core:2.12.0
//> using dep org.typelevel::cats-effect:3.5.4
//> using dep org.scalatest::scalatest:3.2.19

Then start the REPL session and play:

$ scala-cli repl test.scala

scala> import cats.syntax.all.*

scala> val someInt: Option[Int] = Some(3)
val someInt: Option[Int] = Some(3)

scala> val someLong: Option[Long] = Some(4L)
val someLong: Option[Long] = Some(4)

scala> someInt. map2(someLong)((i, l) => i. toString + l. toString)
val res0: Option[String] = Some(34)

You can define the dependencies and Scala code in the same file and run it:

//> using dep org.typelevel::cats-core:2.12.0
//> using dep org.typelevel::cats-effect:3.5.4

@main def main = {
import cats.syntax.all.*

val someInt: Option[Int] = Some(3)
val someLong: Option[Long] = Some(4L)

val res = someInt.map2(someLong)((i, l) => i.toString + l.toString)
println(res)
}

Then run the file:

$ scala-cli run test.scala