Learn Scala 3 The Fast Way: Why Learn Scala 3?

I hope you already have some idea of what Scala 3 is good for, but if you don’t, let me give you my completely biased opinion! :)

Scala offers a fusion of FP and OOP

Way back in 2010, technologies like Google Maps, Gmail, Facebook, and Twitter were all relatively new, and pretty much the only FP language anyone had heard of was Haskell. While I was wandering around Alaska, this was when I first learned about Scala, and I learned that a distinguishing feature of it is that from its origin it has been a fusion of FP and OOP.

Martin Odersky is the creator of Scala, and if you don’t know him, he studied under Niklaus Wirth, who created several programming languages, including Pascal, which was often used as a teaching language in colleges in the 1990s. Mr. Odersky originally became known to me (and many others) as the person who brought generics to Java in Java 5.

After that he created a research language named Pizza, and that work led him to create Scala. When he created it he strongly believed that a fusion of FP and OOP was possible, and has stated it like this:

The essence of Scala is a fusion of FP and OOP in a typed setting, with functions for the logic, and objects for the modularity.

As this book progresses you’ll see examples of what this means. But for now, just know that this Fusion of FP and OOP is a hallmark of the Scala language.

Scala is expressive

Scala is a concise language, but more importantly, it’s expressive. This means that you can get a lot of meaning across in a small amount of characters, but you can also come back to your code in the future and still read it.

For example, even though you may not have seen any Scala code before, I think you can look at these examples and see that there are no unnecessary characters in these examples, but they’re all very readable:

val a = 1
val b = "two"

// a "for loop"
for i <- 1 to 3 do println(i)

// a scala method
def min(a: Int, b: Int): Int =
    if a < b then a else b

Sometimes when a language is advertised as concise it really means terse, which means that it will be hard to read later. But as you’ll continue to see, Scala is expressive, not terse.

Scala is consistent

On the official Scala website I wrote the pages that compare Scala to other programming languages, and that work led me to realize that Scala is more consistent than other programming languages. This is important, because it means that you don’t need to learn a lot of new concepts or weird variations of syntax for different conditions. For instance, this is the way you create some common data structures in Scala:

val a = List(1, 2, 3)
val b = ArrayBuffer(1, 2, 3)
val c = Set(1, 2, 3)
val d = Map(1 -> "a", 2 -> b)

Notice that each type of data structure is created the same way. This may seem like a small point, but in other languages data structures are created with (), [], and {} symbols, while in Scala they are all just classes. You’ll find this same consistency throughout the Scala language.

A terrific JVM language

Simply put, IMHO, Scala is the best programming language available on the Java Virtual Machine (JVM). The name “Scala” comes from the word scalable, and true to that name, it’s used to power some of the busiest websites in the world. Scala is used by Apple, Disney, GM, Starbucks, Tesla, The Guardian, Twitter, and in many, many more products and companies.

Using Scala you can create server-side applications using frameworks like the Play Framework, and many others. The Akka library has the best “actors” library in the world, and they also offer a serverless computing solution.

There are many other Scala libraries and frameworks, and these — along with other FP libraries listed below — power some of the most high-performance websites in the known universe!

The most modern FP libraries

Scala is also the home of two of the world’s best and most modern functional programming libraries in Cats and ZIO. It’s amazing to think about it, but these truly are World-Class, Best On Planet Earth FP libraries. At the time of this writing ZIO 2 is just being released, and it doesn’t get any more modern than that.

A JavaScript replacement

Scala can also be compiled to JavaScript using the Scala.js library. This means that instead of writing client-side applications using JavaScript, you can use Scala! For instance, the “exercises” website that accompanies this book is written with Scala.js. (I share lessons on how to get started with Scala.js in the Scala Cookbook, 2nd Edition.)

Native executables

Scala can also be compiled to native executable applications with the Scala Native and GraalVM tools. With these tools, Scala is a terrific language for writing command-line applications and microservices.

A great scripting language

Thanks to a tool named Scala-CLI — which you’ll learn about shortly — Scala is also a terrific scripting language. Personally, I love being able to write server-side applications, client-side applications, native executables, and scripts with the same programming language and its huge ecosystem of libraries.

Lots of libraries

In addition to FP libraries, the Scala ecosystem has many other libraries. You can use your favorite search engine to find libraries for specific tasks, but you can also use these two tools when you’re looking for libraries for a specific task:

The Awesome Scala list is a long list of projects, organized by categories, and shows the number of people who have “starred” projects, and what the projects’ commit activity looks like. Scaladex is more of a search engine for Scala projects that shows even more data in its search results.

Scala will change how you think

As someone said many years ago, a great thing about Scala is that it will change how you think about programming. For instance, when I first used Java in the late 1990s I wrote many, many custom for loops because that’s just the way things were done.

But thanks to the Scala collections classes you’ll see that almost all of those custom for loops fall into certain categories, like this:

  • Filtering
  • Transformational
  • Informational

While in 2010 some people thought the methods on Scala’s collections classes were unusual or overwhelming, these built-in methods are now basically industry standards. These methods mean that instead of writing code like this:

// old way to create a new list from an old list
val newList = LinkedList[Int]()
for
    i <- oldList
    if i > 5
do
    val j = i * 2
    newList ++ j

you do this:

val newList = oldList.filter(_ > 5)
                     .map(_ * 2)

Given the fact that filter and map are both de facto industry-standard methods, which code would you rather read?

I hope you’ll agree that the second example is better, simpler, and still very readable. And if you’ve never seen anything like this before — fear not — you’ll know how to read and write it by the end of this book!

If you want to work on “big data” applications, it may also help to know that this is how you write code with Apache Spark.

books by alvin