Virtuous Programmer Adventures of an Autodidact

6Dec/100

Scala 1/4: Getting Started

This is the first article in a series, to read the whole series go here.

Scala

"The work on Scala stems from a research effort to develop better language support for component software." -Scala Overview

Scala is:

  • Object Oriented
  • Functional
  • Strongly Typed
  • A Research Language
  • JVM Based

Why Scala is Interesting

The overall theme of Scala seems to be the same as the theme with any building toy: make it possible to create interesting things from relatively simple things. While studying it I've been repeatedly surprised by finding language constructs that are not from the core language, but can be created from it. For example, Erlang is well known for its actor model for concurrency. With Scala it's possible to create such a system in libraries.

Purely Object Oriented... and Functional?

Scala is both object oriented and functional. My first reaction was that this is a contradiction in terms. The minimum requirement for a language to be functional is that all functions are first class objects. While there are a lot of ideas attached to object oriented languages, at the core an object oriented language has two basic attributes:

  1. Every element in the language is an object.
  2. Every action is performed by a method call from one of those objects.

Scala resolves this apparent contradiction by making functions objects in the object oriented sense. Each function is a singleton object with the single method apply which is executed when the function is called.

Powerful Pattern Matching

Most programming languages have some variant on:

(pseudo code)
switch val:
  "first" -> res1
  "second" -> res2

Which is syntactic sugar for:

(pseudo code)
if val == "first":
  res1
elif val == "second":
  res2

Scala's match functionality is a generalization of this pattern that applies to classes as well as values, for example if you first create a couple of case classes:

  1. case class Node(left: Tree, right: Tree) extends Tree
  2. case class Leaf(num: Int) extends Tree
  3.  

You can then create a function that will sum the tree:

  1. def sumTree(tree: Tree): Int = tree match {
  2.   case Node(l, r) => sumTree(l) + sumTree(r)
  3.   case Leaf(n)    => n
  4. }
  5.  

This will look familiar to those who've coded in languages with Algebraic Type systems like Haskell. This is a case of Scala taking two relatively common elements, classes and switch statements, and combining them into something that is more powerful.

Access to Java's Libraries

One of the major problems with any programming language is having sufficient libraries to be useful for real tasks. For a language to have these on its own it must either have a large community to produce the libraries over time or an exceptionally dedicated small community. Scala solves this problem by having transparent access to Java's libraries, and thus the work of Java's very large community. Any Java library can be imported into a Scala program as though it were one of Scala's native libraries. Strictly speaking since Scala compiles to Java bytecode, it is.

Scala Installation

Before you install Scala, you'll most likely want to make sure you have Java's JDK installed. You can pick it up here.

Scala doesn't have a true installer, so you have to do a bit of manual work to get started:

  1. Download the zip file from Scala Downloads.
  2. Unzip the file into your C:\ directory.
  3. Renaming the resulting directory to something like "scala" may make your life easier if you have to upgrade later.
  4. Add "C:\scala\bin" to your path in environment variables.
  5. If you have Java installed and you want to be able to run Scala programs using Java, add "C:\scala\lib*" to your CLASSPATH environment variable.

Hello World

Scala as Script Interpreter

Scala has a double life, it's both a compiler for Java bytecode and a scripting language. When it's acting as a scripting language the rules are a little more flexible. Compiled, Scala's syntax requires that everything be contained in an object or class. When run through its interpreter you can just have sequences of commands. For example:

HelloWorld1.scala:

  1. println("Hello World!")
  2.  

You can run this program from the command line with: scala HelloWorld1.scala

Scala as Compiler

HelloWorld2.scala:

  1. object HelloWorld {
  2.   def main(args: Array[String]) {
  3.     println("Hello World!")
  4.   }
  5. }
  6.  

This version of hello world can be run by first compiling it with scalac HelloWorld2.scala then running it in Java's VM with java HelloWorld or in Scala with scala HelloWorld. As with Java, by default the name of the produced class comes from the main class, or here singleton object.

Scala's Interactive Shell

Interactive shell has to be one of my favorite language feature and Scala's is rather nice. To start it up type scala at your command line. From there, you can get hello world by typing println("Hello World").

A Couple of Nice Features

When you type something into Scala's interactive shell you'll notice that the interaction looks something like this:

scala> 3 + 4
res0: Int = 7

You can then use res0 as a variable. Additionally if you type :power you'll get access to several nice exploratory features of the language, for example, type in 7. then tab to get all the methods that apply to an integer.

Trivia

  • Scala stands for "scalable language".
  • Scala was originally designed to be interoperable with both Java and .Net.
  • The leader of the Scala team, Martin Odersky, also helped develop Java Generics.

References:

Posted by Frank Berthold

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.