Another Tour of Scala

ScalaBasics

The Gist

There is no tour element to cover all of this stuff, however Local Type Inference and Predefined function classOf cover some of this. At any rate, I think it’s pretty handy to go over before looking at too much code.

My Interpretation

Syntax

Some basics about Scala syntax that aren’t obvious (especially if you are coming from Java)

Literals and Syntactic Sugar

(There is a lot more syntactic sugar, but this covers some basics)

Structure

You do not need a one-to-one mapping of file to class, as you would in Java. Your .scala files don’t even need to be named like the class they define, or mimic the package structure.

Compiling/Running

scalac and scala work just like their java counterparts.

scalac SomeClass.scala SomeOtherClass.scala
scala -cp . SomeClass

What happens when you run a execute a class like this isn’t quite the same as Java, however. As an example, you can define an object with a main method, like so:

object MainClassObject {
  def main {
    println
  }
}
scala -cp . MainClassObject

(There are other ways to do this, but this is a way)

Read/Eval/Print

Like most scripting languages, and unlike most compiled languages, Scala has a REPL (Read/Eval/Print Loop) where you can play around

scala
Welcome to Scala version 2.7.4.final (Java HotSpot™ Client VM, Java 1.5.0_16).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val a = List(1,4,6,87,5,3,9)
a: List[Int] = List(1, 4, 6, 87, 5, 3, 9)
scala> val b = 12 :: 34 :: a
b: List[Int] = List(12, 34, 1, 4, 6, 87, 5, 3, 9)
scala> b
res0: List[Int] = List(12, 34, 1, 4, 6, 87, 5, 3, 9)
scala> a.map((item) => item * item) 
res2: List[Int] = List(1, 16, 36, 7569, 25, 9, 81)
scala>^D

My Thoughts on this Feature

The good:

The bad:

It’s a win by a long shot (though more advanced topics will deal with some syntax quirks that I just don’t get).