Skip to content

Instantly share code, notes, and snippets.

@dspeele
Last active October 5, 2017 17:54
Show Gist options
  • Save dspeele/79a7a4c78f4c040084fb to your computer and use it in GitHub Desktop.
Save dspeele/79a7a4c78f4c040084fb to your computer and use it in GitHub Desktop.
Getting started- Learn Scala

Installing the JDK

Linux

  • Ubuntu, Debian: To install the JDK using apt-get, execute the following command in a terminal sudo apt-get install openjdk-7-jdk

  • Fedora, Oracle, Red Had: To install the JDK using yum, execute the following command in a terminal su -c "yum install java-1.7.0-openjdk-devel"

  • Manual Installation: To install the JDK manually on a Linux system, follow these steps:

    1. Download the .tar.gz archive from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
    2. Unpack the downloaded archive to a directory of your choice
    3. Add the bin/ directory of the extracted JDK to the PATH environment variable. Open the file ~/.bashrc in an editor (create it if it doesn’t exist) and add the following line export PATH=/PATH/TO/YOUR/jdk1.7.0-VERSION/bin:$PATH

Verify your setup: Open a new terminal (to apply the changed .bashrc in case you did the manual installation) and type java -version. If you have problems installing the JDK, ask for help on the forums.

Mac OS X

Mac OS X either comes with a pre-installed JDK, or installs it automatically.

To verify your JDK installation, open the Terminal application in /Applications/Utilities/ and type java -version. If the JDK is not yet installed, the system will ask you if you would like to download and install it.

Windows

To verify the JDK installation, open the Command Prompt and type java -version. If you have problems installing the JDK, ask for help on the forums.

Install sbt

Verify that sbt is installed correctly: open the Command Prompt and type sbt sbt-version, you should see the version number of sbt (the first time you run it, sbt will download libraries from the internet). If you have problems installing sbt, ask for help on the forums.

Installing the Scala IDE for Eclipse with the Scala Worksheet (Linux / Mac OS X / Windows)

You can download the Scala IDE for eclipse with the Scala Worksheet pre-installed from the following URL:

http://typesafe.com/stack/scala_ide_download (Make sure to download the IDE for Scala version 2.11.6!)

After downloading the archive for your operating system, simply unpack it and start eclipse. Eclipse requires you to select a workspace on startup. We recommend you create one workspace directory for this class and use it for all assignments.

Hello World: Scala IDE and the Scala Worksheet

To familiarize yourself with the Scala IDE, create a small “Hello World” project in eclipse:

  1. Go to “File” - “New” - “Other…” and select “Scala Project” from the folder “Scala Wizards”
  2. Chose a project name and select “Finish”
  3. Select “File” - “New” - “Scala Object” to create a new object
  4. Enter Hello as the name for the object and put greeter as the package name above
  5. Change the source code to the one given below [1]
  6. Save the file and select “Run” - “Run” from the menu. Chose to run as “Scala Application”

You should see a the hello world output in the Eclipse console.

[1] Source code

    package greeter
    object Hello extends App {
      println("Hello, World!")
    }

Creating a Scala Worksheet

Creating a Scala Worksheet is very easy:

  1. Right-click on the package greeter in the hello world project that you just created
  2. Select “New” - “Scala Worksheet”
  3. Choose a name for your worksheet (different than Hello or the name you chose for the “Scala Object” before)

Now you can type some Scala code into the worksheet. Every time you save the file, the content of the worksheet will be evaluated. Copy the following code into the object of your worksheet:

      val x = 1                                       //> x  : Int = 1
      def increase(i: Int) = i + 1                    //> increase: (i: Int)Int
      increase(x)                                     //> res0: Int = 2

IntelliJ (optional alternative IDE)

  • Download IntelliJ IDEA Community Edition

IntelliJ IDEA Community Edition is an open-source version of IntelliJ IDEA, a premier IDE for Java, Scala and other JVM-based programming languages. You can download it from here

  • Install Scala plugin

Before you create or open a Scala project, you need to install the Scala plugin. For that, use the Configure → Plugins → Browse JetBrains Plugins from the Welcome Screen or Preferences (Settings) → Plugins

Note that Scala plugin requires restart to complete installation.

  • Creating a project

The easiest way to create a project is to use the Project Wizard. To use it, Click Create New Project on the Welcome Screen, then select Scala, and finally SBT Project.

Click Next to specify project name and location. Once you’ve entered this information, IntelliJ IDEA will create an empty project containing a build.sbt file.

  • Creating a Scala worksheet

Simply use the Create New action from context menu or press Ctrl+N on a Scala source root.

To evaluate worksheets, use the corresponding toolbar icon, or press Alt+Ctrl+W (Alt+Cmd+W on OS X)

  • Creating a Scala class

Much akin to worksheets, Scala classes are created via context menu action Create New, or by using the Ctrl+N shortcut.

Once you are ready, run your application by pressing Ctrl+Shift+F10, or using the editor context menu.

After the application has finished running, you’ll see its output in the Run tool window.

  • Opening an SBT project

To open an SBT project in IntelliJ IDEA, go to the Welcome Screen, click Import Project, and select SBT build file that you wish to open. IntelliJ IDEA will then create a new project and import the selected file to it.

Also, you can open an SBT project without calling the Import Project action. Just click Open Project from the Welcome Screen and select an SBT build file.

  • Synchronizing SBT and IntelliJ IDEA projects

IntelliJ IDEA SBT support synchronizes the project with your build file, so when you change Scala version you're going to use, or add a library, your project is updated accordingly.

  • Using terminal to run SBT commands

The easiest way to run SBT commands from from IntelliJ IDEA is to use the Terminal tool window via Alt+F12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment