Typesafe Activator

Hello Scala!

Hello Scala!

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 an updated ScalaTest, and the sbt-eclipse plug-in.

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-eclipse> activator ui

This will start Typesafe Activator and open this template in your browser.

Option 3: Create a hello-scala-eclipse 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-eclipse on the command line.

Option 4: View the template source

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.

Defining Values

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:

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:

Have a Question?

From the blog

The Typesafe crew is thrilled to share that Scala Days SF was just fantastic. A big thanks to all who attended. We were wowed by our awesome keynoters, speakers and volunteer staff, and it was great to feel the excitement and energy at the beautiful Fort Mason.

After an inspiring Scala Days (the next one is in Amsterdam), it's great to be able to shine some light on technologies dedicated to improving the workday of Scala developers. We recently talked about eight hot technologies that perhaps you didn’t know were built in Scala, and in the spirit of that we’re happy to highlight Takipi, a company that's making life for commercial Scala apps better. Branching out from Java, Takipi now helps Scala developers understand when and why their code breaks in production. For more details, we asked Josh Dreyfuss, who recently joined the Takipi team, to take us through it all. -Oliver White, Typesafe, Inc.