Scala

From Wikibooks, open books for an open world
Jump to: navigation, search
Scala
A Pragmatic Programming Language

Scala is a general-purpose language designed to support clear, concise and type-safe programming as well as to scale from the smallest applications to the biggest. It does so by providing a sophisticated type system, a concise and flexible syntax supporting internal DSLs, and an integration of the functional and object-oriented paradigm together with many powerful abstractions. Furthermore, Scala is compatible with Java, and existing Java libraries can be called from Scala and vice versa. The consequences of this design are many:

  • The number of lines required for the same code is often much smaller in Scala than for other general-purpose languages such as C, C++, C# and Java, typically ranging from 2-3x to 10x. This is done while still having performance comparable to Java.
  • Many common tasks that normally require either verbose syntax, built-in syntax or external DSLs such as actors, linear algebra, numerical computing, testing, parsing, etc. can instead be provided as libraries in Scala with concise syntax. Examples include the actor libraries such as Akka; numerical programming, linear algebra and more such as Breeze; testing such as ScalaTest or specs2; or parsing such as the parser combinator libraries.
  • Performance, safety and productivity are 3 areas that are difficult to obtain in one programming language. Scala provides all three to a high degree. Scala has the productivity commonly associated with scripting languages, while having performance comparable with Java. The static and sophisticated type system, the powerful abstractions and the support for referential transparency and immutability help increase the safety, clarity and reliability of programs written in Scala.
  • Considerable support for parallel and concurrent programming. Examples include actor libraries, immutable collections, parallel collections and STM (see Akka).
  • A gradual shift from Java to Scala is supported. Scala can be introduced step by step, for instance by writing new test modules in Scala.

As of 2012-07-05, the current implementation of the language is actively being developed for the Java Virtual Machine, and there are efforts underway to support the .NET framework.

Due to the depth of the language, the many powerful abstractions, and the sophisticated type system, Scala is sometimes seen as a language that is difficult to learn. While parts of Scala are definitely challenging for most programmers, the knowledge and skills required to be effective in Scala is much easier to attain. This book divides the different topics into layers of knowledge that are required for doing different things in Scala. The layers are not always indicative of difficulty. For instance, higher-order functions are seen by some to be difficult, but because they are extremely useful, they are described in the Basic part of the book.

If you are a Java programmer, there exists several guides, resources and books that are directly focused on Java programmers. See http://www.scala-lang.org/node/1305#Java for more information.

Overview

The book is split up into several parts: Basics, Intermediate and Advanced.

"Basics" describes how to setup Scala, and introduces basic syntax, expressions, functions, basic object-oriented programming, and basic functional programming. It also introduces List, case classes and pattern matching. The articles in Basic contains sufficient knowledge to write simple programs in Scala.

"Intermediate" expands upon the knowledge in Basics. This includes advanced functional and object-oriented programming, type-safe programming and an introduction to the collections library. The knowledge in Intermediate should be sufficient for using Scala effectively for many applications.

"Advanced" describes features that are useful for writing libraries, such as higher-kinded types and implicits.

The book should have short, motivating and easy to understand articles on the different subjects. The articles should not spend too much time explaining why features are the way they are, or describing every last detail. The reason is that the book is meant to be a learning resource, and covering every last detail that is not very important may hinder learning and waste the readers time. For readers that desire more comprehensive coverage and documentation, there exists several resources such as http://docs.scala-lang.org/, Programming Scala from O'Reilly, and Programming in Scala from Artima.

Basics

First steps

Object-oriented programming 1

Functional programming 1

Lists, case classes and pattern matching

Intermediate

General

Object-oriented programming 2

Functional programming 2

Types 1

Collections

Optional

Advanced

General

Types 2

Additional Resources