Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Scala for C# Developers: A Tutorial

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.

Selecting the workspace folder for Scala
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.

Specifying the Scala project options
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.

Checking the libraries included in the new Scala project
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).

Package Explorer in a Scala project
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.

Context menu in a Scala project
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.

A new object in the source code editor and in the Outline panel
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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.