One of the very first data structures used in every language is the array. Scala provides two types of arrays, implemented using the JVM facilities: the Array class and the ArrayBuffer class. The first one is used when the length of the array is known in advance, while the second one provides a dynamically growing type of array.

Here I'm going to declare a new Array:

scala> val ary = Array[Int](1, 2, 3)
ary: Array[Int] = Array(1, 2, 3)

Scala inherited the concept of extensions from Java. However the syntax is much clearer then its predecessor counterpart:

val url = new URL("")
try {
  ... something ...
} catch {
  case _: MalformedURLException => println("Where did you send me?")
  case ex: IOException => ex.printStackTrace()

This example is taken with some minor modifications from the wonderful book written by Cay S. Horstmann "Scala for the impatient". This book is such a wonderful introduction to Scala that Martin Odersky (Scala creator) asked Horstmann to freely release the first chapters to speed up Scala's adoption. I'm reading it and I really recommend it to everyone.

As I've noted before, Scala is so functional that every instruction has a value, as it happens in math. Scala is also functional in avoiding the standard three-elements-loop construct:

/* C/C++/Java classic for loop */
for (initial condition; comparative condition; incremental instruction) { ... }

This was considered so wrong by Scala designers that they purposely avoided including this construct. Since Scala has while loops, is always possible to replace this missing construct with a classic:

var variable = <initial state>
while (variable != <end state>) {
    ... do something ...
    variable += 1

This is however considered bad programming in Scala. Every book would suggest another form on this task:

for (i <- <initial> to <final>) { ... }

Being a professional programmer since two decades (and being a programmer since three - wow!) I've learned a lot of languages. I've started when I was a child with Logo, then moved to BASIC. After that it came Pascal and later Modula-2. I've learner Tcl on Solaris, then discovered Perl and forgot everything else for a long while. I've done some Javascript on the web and some Java programming on the Android platform too. I've coded a lot in C (especially tagsistant and magma) and I've done some Joomla development in PHP.

Each of those languages has its particularities, that's for true. Perl and Tcl are interpreted dynamic languages for fast and furious programming, while Java is bloated enough to fulfill the requirements of enterprise class bureaucracy. C is the boss in the downtown of the hardware with its slim'n'lean functions and PHP has the right mix of functions, classes and chaos to cook a web page with perfectly burnt corners.

But all of them are imperative, or at least are usually used in the imperative way. And then enters Scala!

  The Cog In The Machine On Which All Depends