Hello Scala World

Scala: Object-Oriented Meets Functional

A good starting point is the official: Getting Started.

First let's check the basic requirements: Java and sbt (a build tool for Scala) both should be installed with recent versions.

$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

sbt: The interactive build tool

You can download sbt or use Homebrew

brew install sbt@1
==> Summary
🍺  /usr/local/Cellar/sbt/1.0.2: 482 files, 61.0MB, built in 49 seconds

Check the installation within a temporary project directory with:

sbt about
[info] This is sbt 1.0.2
[info] sbt, sbt plugins, and build definitions are using Scala 2.12.3

Creating your first Scala/sbt based project with IntelliJ is pretty straight forward and well described in Building a Scala project with IntelliJ and sbt.

Once you created your first Scala class (or better object)

package de.datenkollektiv.sandbox.scala

object HelloScalaWorld extends App {
    println(List("Hello", "World").mkString("", ", ", "!"))

you can run it with the usual shortcut <^> + <⇧> + R.

There is also a nice integration with sbt and IntelliJ via the Terminal window (use <⌥> + F12 to open)

$ sbt
[info] Loading settings from plugins.sbt ...
[info] Loading project definition from ~/sandbox/hello-scala-world/project
[info] Loading settings from build.sbt ...
[info] Set current project to hello-scala-world (in build file:~/sandbox/hello-scala-world/)
[info] sbt server started at
sbt:hello-scala-world> clean
[success] Total time: 5 s, completed Sep 27, 2017 12:03:33 AM
sbt:hello-scala-world> compile
[info] Updating {file:~/sandbox/hello-scala-world/}hello-scala-world...
[info] Done updating.
[info] Compiling 1 Scala source to ~/sandbox/hello-scala-world/target/scala-2.11/classes ...
[info] Non-compiled module 'compiler-bridge_2.11' for Scala 2.11.8. Compiling...
[info]   Compilation completed in 7.664s.
[info] Done compiling.
[success] Total time: 10 s, completed Sep 27, 2017 12:03:46 AM

Besides the tasks clean, compile, test and run there is the usefull console task which allows to start the Scala console via sbt (use <^> + D to get back to you sbt session).

To add a new dependency (e.g. for testing) modify the build.sbt accordingly. In our hello-scala-world project this looks something like:

name := "hello-scala-world"

version := "0.1"
scalaVersion := "2.12.3"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

To get started with testing your code you might want to check out: Testing Scala in IntelliJ with scalatest

Happy Scala coding!

Show Comments