How to Get Started with Programming in Scala

Creating Applications with Scala Computer Programming

© Mark Alexander Bain

Aug 14, 2009
How to Get Started with Programming in Scala, Mark Alexander Bain
Scala is a free, functional, object oriented programming language for the experienced Java programmer and it is even suitable for programming for beginners

There's no doubt that President Obama will go down in history for many things, and one of those things is that the online traffic during his inauguration helped Twitter to decide to move to the Scala programming language for their message queueing system.

And the great thing is that any programmer can start to understand why that was because this object oriented functional programming language is completely free to download and use.

Obtaining and Installing Scala

The Scala programming language can be obtained from http://www.scala-lang.org. It does not need installing, just unpacking into a suitable directory. However, it will need some system environment variables setting. These are:

  • SCALA_HOME - this must point towards the directory into which Scala has been unpacked
  • PATH - this must be updated to include the Scala bin directory

It will also require the Java runtime to be installed, although it is worth noting that some IDE's (Integrated Design Environments) already support Scala development, and there are Scala plugins for:

  • Eclipse
  • IntelliJIDEA
  • Netbeans

However, all that's actually needed is a console (such as a Linux shell or the Windows command prompt) and a simple text editor.

Starting Scala in Interactive Mode

Once the system environment variables have been set up correctly then Scala can be started in interactive mode by going to the command line and typing:

scala

This will start an interactive session and the programmer can start programming in Scala, in this case to create a simple "Hello World" object:

object Hello {
def main (args: Array[String]) {
println ("Hello World")
}
}

In this example Scala will respond that it's "defined module Hello", and then the programmer can run the code:

Hello.main (null)

And the text "Hello World" will be displayed on the screen (as can be seen in figure 1). It's worth noting at this point that Scala is case sensitive, and so the following will result in an error message being displayed:

hello.main (null)

Of course, regardless of the case of the letters, this is an impractical way of developing a full Scala application. It is only really suitable for testing and training purposes. In programming Scala, therefore, the programmer must look at other ways of running the code.

Compiling and Running Scala Code

If the code for the "Hello" Scala object is saved to a file named "Hello.scala" then the programmer can use the scalac program to compile it:

scalac Hello.scala

And then it can be run:

scala Hello

It will display the same text as before (as shown in figure 2), but now the programmer can carry on programming in Scala and then passing their applications on to all of their users to enjoy.


The copyright of the article How to Get Started with Programming in Scala in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish How to Get Started with Programming in Scala in print or online must be granted by the author in writing.


How to Get Started with Programming in Scala, Mark Alexander Bain
Figure 1: Interactive Scala, Mark Alexander Bain
Figure 2: Compiling and Running Scala, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo