There are a couple of ways to get started with Scala.
scalac
compiler.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 type:
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 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.
maven-scala-plugin
.scala
plugin (available in the JetBrains repository).bin
folder on your path.