Chapter 2. Scala – Taking the First Step

Play! Framework in its second version has been implemented using the programming language Scala. That is, the whole core is Scala based, but APIs are available in both Java and Scala (without closing the doors on other JVM languages in the future).

If we're able to keep Java as the programming language of our web application, the template system is still a Scala one. Hopefully, the scope of a templating system shouldn't include business logic, as a result of which the needs are often quite simple and recurrent.

This chapter's intent is to provide a very high-level view of what Scala is, without going deeper into the details. Following are the topics that will be covered:

  • An introduction to what Scala is
  • Scala expressions versus Java statements
  • A taste of the Scala type system
  • How to get the best from sequences in Scala
  • Partial application of functions—a simple and powerful tool used for composition

Note

The examples will be presented both in Java and Scala to help you to intuitively understand the concepts presented in the Scala version.

Introducing Scala

Scala is such a complete language that it could be defined in several ways. However, we'll try to summarize it with some shortcuts. Scala is a complete language meant to optimize development time and code. That's why the name Scala was chosen, which stands as a mix of scalable and language. The name signifies that the underlying concepts of the language are growing well with application needs or complexities.

Why Scala can be defined as optimized is mostly because of the paradigms on which it relies and the ones it offers.

In short, Scala code is more concise and elegant, and can be less buggy simply by smoothly combining the features from an object-oriented language and a functional one. Very roughly, take a blender, drop in Java and Haskell, and you'll get a taste of Scala.

In the coming sections, we'll see the common features of Scala.

Expressing your code

Scala is a language that uses expressions wherever it makes sense—which is everywhere. Indeed, an expression is an instruction that returns something. So, every construction is expected to return a value. That includes if-else, while, for, and so on.

Let's see them in action and see how helpful it can be. In order to ease the readability of the code, we'll see each example in both Java and Scala, enabling comparison on the fly.

If-else

An if-else statement in Java is a way to alter a behavior based on a predicate, which can be composed of repetitive if-else blocks. Scala has exactly the same objective for if-else but will always return a value. The following screenshot shows some examples:

If-else

Note

For those hungry to copy paste, this code is also provided in the code files of the book. It's recommended that you look at the sources but try them yourself first.

If we focus on the first example, we will see that the Scala version looks like the corresponding if-else statement that Java has using ? and :. However, the Java version doesn't scale very well and you must embed them deeper and deeper, whereas the Scala code remains easily readable. Another good feature is that no extra temporary variable is required in the Scala version.

Switch/Pattern matching

One of the greatest features that Scala brings is its pattern matching against structures (even for complex/multilevel ones). Where Java's switch statement gets stuck with integer and enum data types (String for Java 7), Scala provides pattern matching.

Pattern matching can be seen (roughly) as a general switch statement that returns a value. What we provide is an object and some patterns (type, possible values, and so on) against which Scala will try to detect a match, stopping at the first match or throwing a scala.MatchError exception if there is no match.

The following screenshot shows some examples:

Switch/Pattern matching

The code is pretty straightforward; the Java version of the switch statement is very reductive whereas the pattern matching of Scala can introspect a lot of things, including structures, and strings that use regular expressions.

The important part of pattern matching in Scala is its flexibility. In the second example (Scala version), we saw five interesting things appearing:

  • Data is a case class, a class that can be matched on and that declares its constructor inline
  • The pattern matching is able to match within structures (Data(1, true))
  • The fourth case is mapping the integer to a new variable named x
  • The x variable can be checked further into a case guardian (x < 100)
  • If a value is not of interest to us at some point, we can use _ to discard it

But apart from all this, what is very noticeable is the readability of the code. Where Java's needed several levels (chaining if-else and/or switch statements), the Scala one remains linear.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset