Typesafe Activator

Hello Scala!

Hello Scala!

Typesafe
Source
December 12, 2014
basics scala starter

Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. This very simple Scala application will get you started building and testing standalone Scala apps. This app uses Scala 2.10 and ScalaTest.

How to get "Hello Scala!" on your computer

There are several ways to get this template.

Option 1: Choose hello-scala in the Typesafe Activator UI.

Already have Typesafe Activator (get it here)? Launch the UI then search for hello-scala in the list of templates.

Option 2: Download the hello-scala project as a zip archive

If you haven't installed Activator, you can get the code by downloading the template bundle for hello-scala.

  1. Download the Template Bundle for "Hello Scala!"
  2. Extract the downloaded zip file to your system
  3. The bundle includes a small bootstrap script that can start Activator. To start Typesafe Activator's UI:

    In your File Explorer, navigate into the directory that the template was extracted to, right-click on the file named "activator.bat", then select "Open", and if prompted with a warning, click to continue:

    Or from a command line:

     C:\Users\typesafe\hello-scala> activator ui 
    This will start Typesafe Activator and open this template in your browser.

Option 3: Create a hello-scala project from the command line

If you have Typesafe Activator, use its command line mode to create a new project from this template. Type activator new PROJECTNAME hello-scala on the command line.

Option 4: View the template source

The creator of this template maintains it at https://github.com/typesafehub/activator-hello-scala#master.

Option 5: Preview the tutorial below

We've included the text of this template's tutorial below, but it may work better if you view it inside Activator on your computer. Activator tutorials are often designed to be interactive.

Preview the tutorial

Explore the Code

You've just created a Scala application!  This is a basic "Hello, World" application. Let's look at the src/main/scala/Hello.scala file.

This file consists of a single object called Hello. This object defines a main method, which denotes that it can be run as a standalone application. Let's look into the definition of the main method:

def main(args: Array[String]): Unit = 

The method definition starts with the keyword "def". This is used to define all methods in scala. After def comes the name of the method, in this case "main".

Following the name is the argument list, denoting input parameters to the method. In the case of the "main" method, it takes a single parameter called "args". When the application is executed, any command line arguments will be bundled into an array of string values (or "Array[String]") and passed into the "main" method.

Following the argument list is the return type of the method. For main, this is "Unit" which denotes "uninteresting" return type. It is similar to the "void" type of Java/C/C++ in that we aren't interesting in calling the "main" method for a return value.

Following the return type is an "=" character and then the method implementation. For the Hello.main method, the implementation is the following block of code:

{
  println("Hello, World")
}

This block of code uses the println method to display the string literal "Hello, world!" on the console. We can see this output by clicking on the Run tab.

Running Applications

The result of running the Hello application is shown in the Run tab. On the left-hand side we can see the console output, which is "Hello, world!" for the Hello application.

Let's make changes to the Hello application and see what happens.

First, go back to Hello.scala and change

println("Hello, world!")
to
println("Hello, Activator!")
and select "Save".

Now, come back to the Run tab. After recompiling and running the project, you should see the console output now shows:

Hello, Activator!

Defining Values

Let's go back to Hello.scala and start mucking with the code.

First, we'd like to have our application also say goodbye. Let's add the following line to the end of the Hello main method's body:

println("Goodbye, Activator!")

Save the file and then navigate back to the Run tab, and you should see:

Hello, Activator!
Goodbye, Activator!

Now, we'd like to re-use the name for the greeting and farewell text. To do so, we're going to use a def in Scala. A def denotes a method, or block of re-usable code. Let's modify our Hello src/main/scala as follows:


object Hello {
  val who = "Activator"

  def main(args: Array[String]): Unit = {
    println("Hello, " + who + "!")
    println("Goodbye, " + who + "!")
  }
}

This new code creates a val called "who" which is used in the greeting messages. After defining the value, everytime the "who" name is referenced, the value we've stored will be used.

The + operator is used to join the necessary strings together before passing to the println method. If we swap back to the Run tab, we'll see that the greeting and goodbye messages remain unchanged.

In Scala, a "val" represents a re-usable piece of data or computation that we do not want to re-run. Once set, a val points to the same information forever. In other words, it is immutable.

Testing Applications

No app, regardless of how simple, is complete without a modicum of testing! Let's modify our existing tests to account for the new "who" method we've added.

Navigate to HelloSpec.scala to see our existing test suite. You should see the following:


import org.scalatest._
import org.scalatest.matchers.ShouldMatchers

class HelloSpec extends FlatSpec with ShouldMatchers {
  "Hello" should "have tests" in {
    true should be === true
  }
}

Let's ignore the imports and class definition for now, and focus on the test itself. This test is using the scalatest library. Scalatest provides an elegant way of writing unit tests in Scala. For our purposes, we're using it to define tests of the hello world application. Currently, we have one test:

true should be === true

This test is nested in the categorization/label:

"Hello" should "have tests"

While it's true that we should have tests in our Hello world application, a test that true is true isn't particularly helpful. Let's define a test using the new "who" value in the the Hello object. Modify the test as follows:


  "Hello" should "know who to greet" in {
    Hello.who should be === "Activator"
  }

Save this new test, and make sure it runs. You can see the testing output on the Test tab. Test results are updated automatically after saving a source file, and the build is finished rebuilding your project.

The new test should show up on the list of tests with the name "Hello should know who to greet", and it should be passing.

If we take a closer look in the code, we're accessing the member "who" on the "Hello" object using the "." notation. In Scala, we can reference any member on an object using that object's name, the ".", and the name of the member.

Now that we've define a single test, it's time to dig further into the Scala language.

Next Steps

You're now ready to really start learning how to write application in Scala! While this tutorial presents the absolute basics of creating application in Scala, we don't cover the details of the language. As you explore further tutorials, we recommend toying with code inside of Activator. You may continue using the browser, or swap to a command line experience via the "activator" script in the hello-scala directory.

If you're interested in learning more about Scala, please look through the following links/tutorials/books:

comments powered by Disqus