Scala usage in enterprise applications is growing faster than the number of available developers, so there is an increasing demand for .NET developers to learn Scala using existing C#, VB.NET, LINQ, and F# knowledge as a foundation. In this first article in a series dedicated to Scala for C# developers, I provide an introduction to Scala and its most popular IDE. It is aimed at those who spend their days with Visual Studio, but are interested in learning the increasingly popular JVM language.
Installing the Scala IDE for Eclipse
Scala (pronounced "Scah-lah") is a JVM language that provides a tight integration of functional and object-oriented programming. If you work with the latest C# versions, you use LINQ, closures, lambda expressions, anonymous methods, generics, covariance, and contravariance. In addition, you usually define and pass Action
and Func
delegates as parameters to methods. Thus, you have already mixed object-oriented code with some aspects of functional programming. If you've worked with F#, you will find it even easier to learn Scala.
If you want to start working with the latest versions of Scala, it is best to use an IDE that works with JVM languages instead of Visual Studio. I'll explain how to work with the Scala IDE for Eclipse 3.0.0 because it provides key features that most Visual Studio users expect from an IDE for using Scala in a Windows environment:
- Code completion.
- Error highlighting that catches compilation errors as you type.
- Integrated Scala debugger.
- Integrated Scala interactive interpreter, known as REPL (Read-Evaluate-Print Loop).
- Semantic and syntax highlighting.
- Source code navigation with hyperlinks.
- Support for third-party plugins that add new features.
To get started, you must install the latest stable Java Development Kit (JDK). You can download JDK 7 from Oracle. You must accept the Oracle Binary Code License Agreement for Java SE before downloading the appropriate version. At the time of writing, the latest version is Java SE Development Kit 7u21. For a 64-bit Windows environment, it is necessary to download and execute jdk-7u21-windows-x64.exe
. Because the Scala IDE for Eclipse 3.0.0 is based on Eclipse 3.7 (Indigo), there are some minor issues with JDK 7. However, the issues shouldn't cause you problems when working with the Scala programming language.
Once you have installed JDK 7, you can download and install the Scala IDE. The latest version is available for both Scala 2.9.x and Scala 2.10.x (I recommend installing the version that targets Scala 2.10.x). You can download the Scala IDE SDK for Eclipse 3.0.0 that targets Scala 2.10.x from the Scala website,see Figure 1. At the time of writing, it was necessary to download and unzip a Scala IDE package for a 64-bit Windows environment. Make sure you always download and unzip the latest available stable version. If you are already working with a specific version of Eclipse, you can install a Scala IDE plugin from the update site by following the instructions listed at http://scala-ide.org/download/current.html and just use your existing Eclipse installation.
Figure 1: The Web page that displays the different available versions of the Scala IDE for Eclipse 3.0.0.
Once you unzip scala-SDK-3.0.0-vfinal-2.10-win32.win32.x86_64.zip
, you will find an eclipse.exe
application within the eclipse
folder. If you unzipped the file within a C:\Scala
folder, you can create a shortcut for C:\Scala\eclipse\eclipse.exe
in order to start the Scala IDE for Eclipse.
Working with the Scala IDE for Eclipse
The first time you start the IDE, you must specify the folder in which you want the IDE to store your projects, (also called the "workspace" folder, see Figure 2). You can select the desired folder for the session or activate the "Use this as the default and do not ask again" option to configure it for all future sessions. Select your desired folder, click OK
, and the Scala IDE will display its main window.
Figure 2: Selecting the workspace folder the first time you run Scala IDE for Eclipse.
Now, it is time to create your first Scala project and see how the IDE works. Select File | New | Scala Project in the main menu.
The IDE will display the New Scala Project dialog box. Enter ScalaProject01
for the project name and leave the default options for the project location, JRE (Java Runtime Environment), project layout, and working sets (see Figure 3). This way, the IDE will create a new sub-folder within the previously selected workspace folder with the same name as the project name to include all the files and folders for the new project. In addition, the IDE will create the following separate folders:
src
: Scala source files (*.scala).bin
: Class files (*.class)..settings
: Eclipse preferences.
Figure 3: Specifying the Scala project options.
Click Next and the wizard will display the Scala build settings with many tabs. Click on the Libraries tab and you will see the following two libraries listed (see Figure 4):
- JRE System Library [JavaSE-1.7]. If you are working with a different version of the JRE, you will see a different number.
- Scala Library 2.10.1. The version number might be higher.
Figure 4: Checking the libraries included in the new Scala project.
Click on Scala Library 2.10.1
and you will see all the jar files included in the Scala library:
scala-library.jar
scala-swing.jar
scala-actors.jar
scala-reflect.jar
Click Finish and the IDE will display the new project in the Package Explorer panel. Package Explorer is equivalent to the Solution Explorer in Visual Studio. The new project (ScalaProject01
) includes an empty src
folder and the two previously mentioned libraries (Figure 5).
Figure 5: Package Explorer displaying the structure of an empty Scala project with just two libraries.
Next, right-click on the project name (ScalaProject01
) in Package Explorer and the IDE will display a context menu (see Figure 6). Select New | Scala Application and the IDE will display the New Scala Application dialog box. Enter FirstApplication
in Object name
and leave (default package)
as the selected option in the Package dropdown list.
Figure 6: A context menu displaying the different new elements that you can add to the selected project.
Then, click Finish. The IDE will add a new FirstApplication.scala
file to the src
folder and display the source code in a new tab (Figure 7). The Outline panel will display the details for the new object to help you navigate through its different elements.
Figure 7: A new object in the source code editor and in the Outline panel.
The initial skeleton code for FirstApplication.scala
is:
object FirstApplication extends App { }
The initial code declares an object named FirstApplication
that derives from the App
trait. Singleton objects are first-class citizens in Scala; therefore, you can just declare objects as singletons without having to create a new type, instance, and its provider as you would in C#. This way, Scala reduces unnecessary boilerplate code for declaring a singleton object. The code defines an instance that inherits from the App
trait and can override the necessary methods.
Scala Conventions
Scala supports classes and traits. You can think of traits as equivalents to C# interfaces with additional features. While interfaces in C# are thin, traits can be considered to be rich interfaces. Like interfaces, traits represent a unit of code reuse, but they also allow you to define concrete methods and fields. A trait (or a class) can mix in many traits. The different traits mixed into a class allow you to stack modifications without using multiple inheritance. One clear example of the advantages of traits is that they make it easier to wrap an existing class or trait and enrich it with additional methods. This way, traits provide a new approach to the extension methods introduced in C# 3.0.