Getting Started with Scala

December 27, 2021

Getting Started with Scala

Topics of Interest

Installing Scala on your computer

The Scala installer is a tool names Coursier, whose main command is named cs. It ensures that a JVM and standard Scala tools are installed on your system. Install it on your system with the following instructions:

Download and execute the Scala installer for Windows based on coursier.

Step 1. Execute the Windows installer. It will first check if a JVM is installed on your system:

NOTE: Ensure you add the PATH as shown above. Setting to no will mean the command line arguments will not work:

It will then check if the standard Scala applications are installed:

Press ENTER to continue and Scala should now be installed.

Along with managing JVMs, cs setup also installs useful command-line tools:

  • A JDK (if you don't have one already)
  • The sbt build tool
  • Ammonite, an enhanced REPL
  • scalafmt, the Scala code formatter
  • scalac (the Scala 2 computer)
  • scala (the Scala 2 REPL and script runner)

Create a "Hellow World" project with sbt

Once you have installed sbt, you are ready to create a Scala project. We will be using the command line to create a project, there is the option to use an IDE instead.

sbt is a build tool for Scala. sbt compiles, runs, and tests your Scala code. (It can also publish libraries and do many other tasks.)

To create a new Scala project with sbt:

  1. Open up a cmd prompt and navigate to C:\temp\scala (create these directories if they don't already exist)
  2. Run the command to create a Scala 3 project:
    sbt new scala/scala3.g8
  3. This pulls a project template from GitHub. It will also create a target folder, which you can ignore.
  4. When prompted, name the application hello-world.

Let's take a look at what just got generated:

Open hello-world project

The most popular IDEs to use with Scala are IntelliJ and VSCode. They both offer rich IDE features, but you can still use many other editors.

Using VSCode with metals

  1. Download VSCode
  2. Install the Metals extension from the Marketplace
  3. Next, open the directory containing a built.sbt file (this should be directory hello-world if you followed the previous instructions). When prompted to do so, select Import Build

Metals is a “Scala language server” that provides support for writing Scala code in VS Code and other editors like Atom, Sublime Text, and more, using the Language Server Protocol.

Under the hood, Metals communicates with the build tool by using the Build Server Protocol (BSP). For details on how Metals works, see, “Write Scala in VS Code, Vim, Emacs, Atom and Sublime Text with Metals”.

Play with the source code

View these two files in your IDE:

  • build.sbt
  • src/main/scala/Main.scala

When you run your project in the next step, the configuration in built.sbt will be used to run the code in src/main/scala/Main.scala

Run Hello World

If you're comfortable using your IDE, you can run the code in Main.scala from your IDE. Otherwise, you can run the application from a terminal with these steps:

  1. cd into hello-world
  2. Run sbt. This opens up the sbt console.
  3. Type ~run. The ~ is optional and causes sbt to re-run on every file save, allowing for a fast edit/run/debug cycle. sbt will also generate a target directory which you can ignore.

When you're finished experimenting with this project, press [Enter] to interrupt the run command. Then type exit or press [Ctrl+D] to exit sbt and return to your command line prompt.

Next Steps

Once you’ve finished the above tutorials, consider checking out:

Daily Crossword

Fresh Reads

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram