© Toby Weston 2018

Toby Weston, Scala for Java Developers, https://doi.org/10.1007/978-1-4842-3108-1_2

2. Installing Scala

Toby Weston

(1)London, UK

Getting Started

There are a couple of ways to get started with Scala .

  1. Run Scala interactively with the interpreter.

  2. Run shorter programs as shell scripts.

  3. Compile programs with the scalac compiler.

The Scala Interpreter

Before working with an IDE, it’s probably worth getting familiar with the Scala interpreter , or REPL .

Download the latest Scala binaries (from http://scala-lang.org/downloads ) and extract the archive. Assuming you have Java installed, you can start using the interpreter from a command prompt or terminal window straight away. To start up the interpreter, navigate to the exploded folder and type1

  bin/scala

You’ll be faced with the Scala prompt.

  scala> _

You can type commands followed by enter, and the interpreter will evaluate the expression and print the result. It reads, evaluates, and prints in a loop so it’s known as a REPL.

If you type 42*4 and hit enter, the REPL evaluates the input and displays the result.

  scala> 42*4
  res0: Int = 168

In this case, the result is assigned to a variable called res0. You can go on to use this, for example, to get half of res0.

  scala> res0 / 2     res1: Int = 84

The new result is assigned to res1.

Notice the REPL also displays the type of the result: res0 and res1 are both integers (Int). Scala has inferred the types based on the values.

If you add res1 to the end of a string, no problem; the new result object is a string.

  scala> "Hello Prisoner " + res1  res2: String = Hello Prisoner 84

To quit the REPL, type

  :quit

The REPL is a really useful tool for experimenting with Scala without having to go to the effort of creating the usual project files. It’s so useful that the community provided a Java REPL2 as far back as 2013. Interestingly, Oracle followed suit and introduced the official Java REPL called JShell in Java 9 in 2017.

Scala Scripts

The creators of Scala originally tried to promote the use of Scala from Unix shell scripts . As competition to Perl, Groovy, or bash scripts on Unix environments it didn’t really take off, but if you want to you can create a shell script to wrap Scala.

1   #!/bin/sh
2   exec scala "$0" "$@"
3   !#
4   object HelloWorld {
5     def main(args: Array[String]) {
6       println("Hello, " + args.toList)
7     }
8   }
9   HelloWorld.main(args)

Don’t worry about the syntax or what the script does (although I’m sure you’ve got a pretty good idea already). The important thing to note is that some Scala code has been embedded in a shell script and that the last line is the command to run.

You’d save it as a .sh file—for example, hello.sh—and execute it like this:

  ./hello.sh World!

The exec command on line 2 spawns a process to call scala with arguments; the first is the script filename itself (hello.sh) and the second is the arguments to pass to the script. The whole thing is equivalent to running Scala like this, passing in a shell script as an argument:

  scala hello.sh World!

scalac

If you’d prefer, you can compile .scala files using the Scala compiler.

The scalac compiler works just like javac. It produces Java bytecode that can be executed directly on the JVM. You run the generated bytecode with the scala command. Just like Java though, it’s unlikely you’ll want to build your applications from the command line.

All the major IDEs support Scala projects, so you’re more likely to continue using your favorite IDE. We’re not going to go into the details of how to set up a Scala project in each of the major IDEs; if you’re familiar with creating Java projects in your IDE, the process will be very similar.

For reference though, here are a few starting points.

  • You can create bootstrap projects with Maven and the maven-scala-plugin.

  • You can create a new Scala project directly within IntelliJ IDEA once you’ve installed the scala plugin (available in the JetBrains repository).

  • Similarly, you can create a new Scala project directly within Eclipse once you have the Scala IDE plugin. Typesafe created this and it’s available from the usual update sites. You can also download a bundle directly from the scala-lang or scala-ide.org sites.

  • You can use SBT and create a build file to compile and run your project. SBT stands for Simple Build Tool and it’s akin to Ant or Maven, but for the Scala world.

  • SBT also has plugins for Eclipse and IDEA, so you can use it directly from within the IDE to create and manage the IDE project files.

Footnotes

1 If you don’t want to change into the install folder to run the REPL, set the bin folder on your path.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.204