Learning Scala

Learning the basics

The first edition of Programming in Scala has been made available in html on Artima.com. It’s a great book to learn Scala from if you’re interested in learning this language. I should note that this book assumes you are already familiar with at least some programming languages like Java or C/C++. Expect some months worth of work before getting familiar with Scala. Especially concepts related to functional programming and type system which can take years to appreciate. Higher-order-functions/closures should be easier to pick up if you have experience with dynamic languages like Javascript. New concepts in relation to object-oriented programming builds on established ones.

A couple of notes on how Scala might differ

Besides including an ordinary compiler for generating code, Scala can also run code interactively from shell/command. With a properly set up installation, you can start it by typing scala in your shell/command.

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Client VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val (evens,odds) = (0 to 9).partition(item => item % 2 == 0)
evens: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8)
odds: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3, 5, 7, 9)

Syntax is a little bit different than what you might be familiar with. Variables does not follow the int x = 0; convention, but instead uses `declare` `name`: `type` = `expression`.

val x: Int = 0 // a val declared variable can only be assigned once
var y: Int = 1 // a var declared variable can be assigned multiple times
y = 2

Type inference means you don’t need to write out the type for variables (nor semicolons ending expressions).

val x = "Hello" // the type of x is inferred to be String

The object-oriented side of Scala offers the concept of a class’ companion object. This means that the static side and instanced side of a class are separated. The static side is now located in the companion object. This solution also enables the companion object to implement and inherit other functionality without affecting the companion class.

// the companion object
object Transform with MatrixUtil[Double] {
  val Identity = Transform(IdentityMatrix4)
// the companion class
class Transform(private val elems: Array[Double]) {

One last thing about Scala. It is not based on programming using statements as in C-like languages. Every line of code is instead an expression that will yield a value. The resulting type for an expression that would be similar to a statement is Unit who’s only value is (). As being based on expressions you rarely need the return keyword.

// the full syntax version
def say(value: Int): Unit = {
  println("I have a " + value)
  ()  // the Unit value
// the short syntax version
def say(value: Int): Unit = {
  println("I have a " + value) // the type of println is String -> Unit
// the shortest syntax version
def say(value: Int) { println("YEEHAW! I got me a " + value) }

Programming in Scala will give you a thorough introduction to the language. It a fun and expressive language to use, and if you have a small project that targets the Java Virtual Machine I think you’ll be pleasantly surprised with Scala.

Leave a Reply

Your email address will not be published. Required fields are marked *