scala-cli, directives and scripts

scala-cli is the runner of the course, and one command covers the work a build tool does elsewhere: it reads a file or a folder, resolves the Scala version and the dependencies, compiles and runs. The flag --server=false keeps it from starting a build server in the background, which is what a one-shot command wants.

A file and a folder

A folder with a project.scala at its root is a project: the file holds only directives, and the sources sit where scala-cli looks for them, src/main/scala for the program and src/test/scala for the tests. The two files below are the whole project:

project.scala

//> using scala 3.10.1

src/main/scala/Main.scala

@main def main(): Unit = val numbers = Vector(3, 1, 2) println(numbers.sorted)

scala-cli run --server=false .

Vector(1, 2, 3)

The dot is the folder, so a second source file needs no argument: scala-cli compiles every file under src/main/scala. scala-cli compile --server=false . is the same build without the run, and scala-cli test --server=false . compiles and runs the test scope, which the MUnit lesson uses.

Directives

A //> using line at the top of a source is configuration that travels with the code: the version of Scala, a dependency, a compiler option or the JVM the program runs on. The same file run directly is a project of one file:

directives.scala

//> using scala 3.10.1 @main def directives(): Unit = println(scala.util.Properties.versionNumberString) println(System.getProperty("java.version"))

scala-cli run --server=false directives.scala

3.10.1
21.0.13.1

The directive the lesson does not use is //> using jvm 21. It asks scala-cli to fetch a JDK of that version from a JVM index on the network; without a connection it fails before it compiles anything, with Error while getting https://github.com/coursier/jvm-index/raw/master/index.json. A reader with a connection gets the requested JDK; without one, the JDK that is already installed is the one that compiles, and a program reads its version with System.getProperty.

A script

The other shape is a script: a .sc file holds top-level statements, which a .scala file does not allow. A script is what a one-off job is written as, and the file name is free:

hello.sc

val name = "Scala" println(s"Hello, $name") println(1 to 3)

scala-cli run --server=false hello.sc

Hello, Scala
Range 1 to 3

A script has the same directives and the same libraries as a program; the difference is only where the statements live. A project that grows needs an entry point, a package and tests, which is why the course writes @main programs from the next lesson on.

The REPL as steps

scala-cli repl starts an interactive session, which needs a terminal: it reads one expression at a time, prints the value and its inferred type, and keeps the definitions of the session. A session needs a terminal, and a page cannot show one, so here are the steps a reader takes:

  1. Start it in the folder of the project, with scala-cli repl --server=false, and wait for the scala> prompt.
  2. Type an expression and press Enter: 1 + 2 answers val res0: Int = 3, and List(1, 2).map(_ * 2) answers the list. A definition with val keeps its name for the rest of the session.
  3. :help lists the commands of the session, :quit ends it, and Ctrl-D does the same.

scala-cli repl --server=false

not run in this environment

A REPL is the right place for a question about a library method and the wrong place for code that has to be kept: a program in a file is compiled again by every run, and the compiler is the check.

Related

Article author: Arthur Isaev