Things I like about Scala

Since I decided to create a blog, I’ve been thinking about what my first post will be about. I’ve been working with Java for more than 3 years, but a couple of months ago I started to work in a application that is built in Scala. Since Scala is mostly what I’m studying right now, I decided to start this blog with a post about the things that I like about it.

I’m just going to briefly describe each feature. You will find more details about these features in future posts.

Compact syntax

Coming from Java, one of the things you will love is it’s syntax.

// there is no need to define l type since the compiler infers it
val l = List(1,2,3)
// l type is: List[Int]

In contrast to a Java list:

List<Integer> l = Arrays.asList(1,2,3); // don't forget that we need to import Arrays class

Another great example is case classes. Case classes are like regular classes, but the difference is that the compiler generates a bunch of methods automatically for them. Methods like equals, hashCode, toString, setters, getters, and many more, are generated by Scala.

case class Person(name: String, age: Int)

From this single line, we get accessors for name and age fields and methods like: hashCode, equals, toString, and many more. These are methods that we usually need to write explicitly in Java.

Expression oriented

In Scala almost everything is an expression. Things like if-else, for comprehensions, and pattern matching are expressions.

def text(number: Int): String = {
  if(number > 0) "positive"
  else "negative"
}

val list = for(n <- 1 to 3) yield n * 2
// list = Vector(2, 4, 6)

In the text function above, there is no need for an explicit return statement because in Scala the last expression of a block is what gets returned. So, due to the fact that if-else are expressions, the return value of the function will be “positive” or “negative” depending on the input.

For comprehensions

For comprehensions are just syntactic sugar for operations like flatmap, map, foreach, among others.

val sentence = List("This","is","a", "sentence")
for {
 word <- sentence
 letter <- word
} yield letter.toUpper // returns: List[Char] = List(T, H, I, S, I, S, A, S, E, N, T, E, N, C, E)

// the compiler translates this for comprehension into something like:
sentence.flatMap(word => word.map(letter => letter.toUpper))

Maybe you don’t see any benefit from the for syntax in this simple example, but for comprehensions are easier to read when you need to perform more operations.

Pattern matching

The way I see pattern matching is like a tool that helps us extract data from an object:

trait Operation
case class Addition(n1:Int, n2:Int) extends Operation
case class Abs(n:Int) extends Operation

def apply(op: Operation): Int = {
  // pattern matching
  op match{
    case Addition(num1, num2) => num1 + num2
    case Abs(n) => if (n > 0) n else -n
  }
}

This piece of code does not cover all the things you can do with pattern matching, but illustrates the idea behind this powerful construct.

Type aliases

This is a nice feature that helps us to create more readable code. Let’s see it in action in the next piece of code:

type Word = String
type Sentence = List[Word]
type Paragraph = List[Sentence]

// now we can define functions like
def reverse(s: Sentence): Sentence
def filter(par: Paragraph)(predicate: Word => Boolean): Paragraph
def addSentence(s: Sentence, p: Paragraph)

Suppose we want to represent a sentence as a list of Strings where each String represents a word. With type aliases, we can create a meaningful names for these data structures, helping us to create more expressive code.

Well, that’s the list of features that I like so far. This list will probably grow as I get to know the language better.

Starting with Scala was a little difficult because all the concepts and features that didn’t exist in Java, but as soon you get comfortable with the syntax and the functional stuff, you will find that Scala is a great language.

What do you like about Scala?