Scala/Setup
Setting up Scala
The following gives a guide on how to install Scala on different operating systems:
Ubuntu/Debian
- Go into the command line.
- Ubuntu: Install Scala using the command
sudo aptitude install scalaorsudo apt-get install scala, and following the instructions. You may also installscala-docfor the documentation and examples. - Debian: Log in via
su -, then install Scala withaptitude install scalaand following the instructions.
- Ubuntu: Install Scala using the command
Windows
TODO: Describe this section more clearly. Until then, see http://www.scala-lang.org/node/310.
Other OSs
TODO: Describe more OSs, including MacOS. Until then, see http://www.scala-lang.org/node/201.
Using Scala in IDEs
Scala are supported Template:As of in several different IDEs, including Eclipse, NetBeans and IntelliJ IDEA.
Setup pages for Scala plugin:
- Eclipse: http://scala-ide.org/
- NetBeans: http://wiki.netbeans.org/Scala#Get_Started
- IntelliJ IDEA: http://confluence.jetbrains.net/display/SCA/Getting+Started+with+IntelliJ+IDEA+Scala+Plugin
Other IDE support can be seen at: https://github.com/scala/scala-dist/tree/master/tool-support
Running Scala
Scala can be written and run in different ways, by using the interpreter, by compiling, or by running it as a script. The "Hello World!" program will be used to illustrate these different ways. This section is based on http://www.scala-lang.org/node/166.
Interpreting Scala
The Scala interpreter is an interactive shell that supports easy writing and running of commands and programs. The Scala interpreter is generally included in the basic Scala installation and can be initiated from the command-line (typically by writing scala), but is also supported in some Scala IDE plugins (such as Scala-IDE for Eclipse).
Once initiated, the prompt looks like this:
scala>
The simplest way to write "Hello world!" is to write the following command:
println("Hello World!")
println prints the given string and adds a newline. If only print is used, no newline is added. If you write this in the interpreter and press enter, the result should look like this:
scala> println("Hello World!") Hello World! scala>
The println command was interpreted and run, and the result (printing to the terminal) is shown.
There are also special commands in the interpreter; for more information on these, type “:help”.
The interpreter is generally well-suited for small tests and experiments, and less for program development. When learning Scala, the interpreter is often a very useful tool, since it gives immediate feedback. The interpreter is actually a REPL, which you can read more about here: http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop.
Compiling Scala
Scala is a JVM based language, and like Java it is compiled into java bytecode files(.class). Below is the Scala version of Hello World:
//hello.scala object HelloWorld { def main(args: Array[String]): Unit = println("Hello world!") }
We can simply use scalac hello.scala to build and then there would be two class files: HelloWorld.class and HelloWorld$.class. Using scala HelloWorld to execute and the screen should print:
Hello world!
There is also a fast compiler for Scala, fsc,which is a tool that submits Scala compilation jobs to a compilation daemon(by default, the scalac/scala will also use fsc). scalac is powerful. It supports other options like -classpath,-verbose, also it provides advanced options beginning with -X and private options beginning with-Y.
Scripting Scala
We can use scala as a script.For a scala source file script.scala
println("Hello "+argv(0))
We can use scala script.scala world to run it, just like the Python script. However Template:As of, there is no support for shebang in Unix like system.
For more REPL tips, you may refer to this page.