Scala isn’t Hard: How to Master Scala Step by Step

Intro

With Scala 3 out in the wild for a few months, we’re now in a new era of Scala development. In this article, we’ll explore why people still consider Scala to be a hard language to learn, then we’ll debunk that myth. I’ll show you, step by step, how to master Scala from scratch, using the exact sequencing that I used for myself when I learned it. This is also how I personally train professional software engineers – and I’ve had the privilege of holding sessions at Adobe, Apple, Orange, and other top companies – with great success over sessions of 2 or (sometimes) 3 days.

You heard that right – you can master Scala in a weekend if you’re committed and follow the steps I show you below.

Why Does Scala Seem to Be Hard?

The fact of the matter is, no programming language is trivial. Every single one has its exciting features and frustrating bits. Scala is no different. But like any other programming language, learning Scala can be done quickly.

From my experience teaching 41000+ people online at Rock the JVM, one common frustration that people have over (learning) Scala is that Scala has too many features and it’s hard for people to connect them all. I can certainly agree: if you learn chaotically and don’t have a structure and sequencing already done, it’s extremely hard to internalize anything. Type classes? Implicits? Givens? Higher-kinds? Of course it’s hard when you have 3 million features in front of you and don’t know which ones to pick first. Another frustration is the “getting started” part and getting something to “just work”. This is what makes many people let go of Scala quite early in the process.

So that’s exactly what I want to help with. In the rest of this article, I’m going to show you exactly what to focus on, and in what order, to learn Scala by yourself.

As a prerequisite, you should be familiar with programming in general – with at least some other language – and have your computer science basics solid. If so, you can spend 1-2 hours on each of these sections in order, and I promise you’ll be ahead of most people learning Scala.

#1 Absolute Essentials

If you’re on board for this journey, let’s warm you up. This is where most people drop Scala because it’s either 1) too frustrating because they have gaps in computer science basics, or 2) too boring because most tutorials spend too much time on these topics.

The absolute basics of the Scala language include a few key ideas. 

  • Defining values (val) and understanding that values are the equivalent of constants in other languages.
  • Getting familiar with types in Scala: this is also where you get accustomed to the compiler and to type inference, the ability of the compiler to figure out the type of your values depending on what you write on the right hand side of an assignment.
  • Defining functions and recursion. This is a key concept: you must learn to think of repetition in terms of recursion instead of loops. This is one of the hardest concepts to internalize, for a beginner.
  • Learning a few non-essential, but fun tricks in Scala, like String interpolation.

#2 Object-Oriented Programming

Most of you know this already, but it’s worth stressing again: Scala is not just for functional programming. In fact, the blend of FP and OOP is what makes Scala so appealing in practice. After understanding the basics, this is your next stop. You should learn:

  • Classes, instances, fields and methods: this is your opportunity to make connections between Scala and your current experience (Java, Python, C++, C#, etc).
  • Method notation and special methods e.g. apply: this is one aspect that makes Scala extremely expressive. It was certainly the most fun thing to learn Scala when I started. The fact that you can say mary likes "The Godfather" instead of mary.likes("The Godfather") was music to my ears. I’m not going to give too many spoilers here.
  • Traits, abstract classes, inheritance and anonymous classes: another great opportunity to map existing concepts from other programming languages – e.g. interfaces and similar structures in Java.
  • Enums: one simple, but much needed feature that was added to Scala 3 and brings Scala to the same level with the most popular programming languages.
  • Case classes: a simple concept with powerful ramifications, because we use lightweight data structures in our code every day. So powerful that other languages e.g. Java and Kotlin have replicated this feature in the form of records and data classes respectively.
  • Exceptions: a similar mechanism for error signaling and handling, because of course Scala is based on the JVM.
  • Generics (just basics for now): the ability to reuse the same code on potentially unrelated types (e.g. collections). Scala takes this Java concept a few million miles further, but stop at the fundamentals for now.

#3 Functional Programming Principles

This is where most people dive in too early, but in my experience, this is not the right first step. The previous two sections serve as foundations for what you’ll discover here.

Why?

Because Scala, being based on the JVM – which was built for Java, one of the canonical OO languages – has to resort to certain tricks to achieve some of the key aspects of functional programming: the ability to pass functions as arguments and to return them as results. In other words, the ability to handle functions as “first class” elements of the language, built for a platform that only supports OOP, needs some OO foundations.

  • Now you’re ready to understand what a function value is in Scala. You can get familiar with the FunctionN family of traits in Scala. With this knowledge, I personally felt like a new section of my brain was unlocked, when I learned this.
  • Learn lambdas, i.e. anonymous functions. With the idea above, this concept will now feel trivial.
  • Now that you can pass functions as arguments and use them like any other values, learn the concept of higher-order functions. Play with HOFs like map, flatMap, filter and learn the essential bits of the collection library.
  • Learn for comprehensions. If you know map, flatMap and filter, you’ll understand this syntax sugar easily. Invent your own data structures – or replicate existing ones – and use for comprehensions on them.
  • Learn a few useful data structures like Option and Try. They will prepare you for further abstractions down the line.

#4 Pattern Matching

The previous sections gave you the absolute basics to work with Scala on a daily basis. From now on, you’re ready to dive into either more abstract/complex topics, or explore more Scala-specific features.

Pattern matching is one Scala-specific feature that has been around for more than a decade, and remains far ahead of the attempts of other languages cough Java cough to replicate it.

Learn what a pattern matching expression is and why it’s useful. Deconstruct case classes and constants first, that will cover 90% of your use case in practice.

After that, if you have some spare time and you’re ready to show off to your Scala teammates, learn some nifty tricks with pattern matching.

At this point, you can be a productive Scala developer for simple to medium projects. The topics above are what I teach in my Scala 3 Essentials course on Rock the JVM, and you can also follow the above steps on your own to learn by yourself in just a day of focused work.

# 5 Advanced Functional Programming

After getting the foundations straight, you should now be ready to dive into more abstract and complex topics. Some advanced functional programming concepts are in order:

  • Partial functions
  • Partially applied functions and a compiler process called eta-expansion
  • Lazy evaluation

With these Scala features, you will unlock some new mental models around programming concepts you’ve been using all the time. For example, I regularly teach people to think of collections as functions. This usually blows their mind as much as it did mine when I first discovered it. Many Scala collections, considered mathematically, are (total or partial) functions mapping domains. This will prepare you for further abstractions down the line.

#6 Functional Asynchronous Programming

We haven’t discussed threads so far, and for good reason. At this point, you are ready to explore the JVM threading model and the kind of problems that we encounter on a daily basis:

  • Creating threads
  • Creating and managing thread pools, submitting tasks
  • Race conditions and synchronization
  • Deadlocks and livelocks

Some of you Java gurus might want to jump over these, as they are exactly the same for Java. However, with these JVM threading foundations in place, you can now explore Futures in Scala: a functional-programming-ready primitive that allows you to write parallel and concurrent code while keeping the functional programming principles intact.

#7 Contextual Abstractions

In this section, we can dive into more abstract territory. Here’s what to study:

  • given instances and using clauses. These might come across as a bit unintuitive at first, because you might be thinking, “why on earth would I need this?”. Trust me, this unlocks some crazy new potential. If you’re learning Scala 2, search for implicit val and implicit arguments – don’t search for other implicit stuff.
  • Extension methods. This is the capability of Scala to add new methods to existing types, even after they’re defined. If you’re an OOP guru and you’re about to shout that this destroys final classes – don’t stress about it. This has nothing to do with OOP.
  • The above features are called “contextual abstractions”. There are more of them, but these are the most important. Learn how to organize them, i.e. how to place them in packages and objects and how to import them effectively. The compiler follows a certain sequence of steps so your code will compile with them. Understand these phases.
  • Learn what a type class is. Build one (or more) out of givens and extension methods. An exercise that I often use is a mini-JSON-serialization library.
  • Optionally, learn context functions and implicit conversions. You might not need them.

#8 Master the Type System

Scala’s type system is almost unparalleled. The language has a million features that allow us to express some extremely general and powerful code – but this, I suspect, is one of the reasons why some people stay away from Scala as well. It doesn’t need to be hard. You need the right structure. Here it is:

  • Learn something called trait linearization in Scala. Because of multiple inheritance, trait hierarchies can get into trouble. Learn how Scala solves that.
  • Go one level deeper with generics, and learn about variance. This feature is widely used in ZIO, one of the most popular libraries for Scala.
  • Learn about variance positions and how to overcome some cryptic compiler errors. You’ll need this, otherwise you’ll spend (like I did) hours banging your head against the wall.
  • Learn about some new types added in Scala 3: literal types, union types and intersection types. These have some interesting properties and will help unify your code and/or your APIs.
  • Learn about self-types. You’ll sometimes notice something like class A { self: B => and wonder if that’s a lambda. It’s not. It looks weird, but it’s quite useful sometimes. Learn the difference between the type restrictions enforced by self-types and regular OO inheritance.
  • Finally, learn about higher-kinded types – the ability to define generic types with type arguments that are themselves generic. This opens the door to some really cool functionality. Coupled with type classes, this feature is at the core of Cats, the popular library for Scala functional programming.

These other four sections are what I teach in my Advanced Scala 3 course, plus a few other bits that are optional if you want to learn Scala really fast. As before: if you follow the above topics in order, you’ll be ahead of 95% of people learning Scala.

#9 Understand the Scala Ecosystem

If you really want to be a productive Scala developer and want to stay – and most new Scala devs don’t want to go back, I know for a fact – the next step I recommend you take is to familiarize yourself with libraries and tools based on Scala. There are quite a few of them and you’ll be able to get pretty much anything done with what the ecosystem can currently offer. Here are some of the most popular:

  • Cats Effect for the foundational layer of concurrent systems, based on pure functional programming. Has an amazingly fast runtime and powerful abstractions that will allow you to write concurrent code with ease. I also teach this at Rock the JVM.
  • Cats for functional programming abstractions. It’s a little more mathematical – it’s a playful name for categories – but they are all rooted in practice and in the need to organize concepts around your codebase.
  • ZIO is a mini-ecosystem of tools for the foundation of concurrent systems. It’s a competitor for Cats Effect, with a runtime just as fast, built on similar principles, but with a different philosophy in terms of code organization and APIs.
  • Akka is a complete set of tools not just for concurrent systems, but for distributed applications, which is super-powerful. It’s built on the actor model, which is what OOP was initially dreamed to be, with amazing results in real critical systems. I’ve created an entire series on Akka on Rock the JVM.
  • Data streaming systems like Apache Kafka are rocking the distributed systems world.
  • Big data libraries like Apache Spark have become the de-facto big data processing tools used everywhere.
  • Play is a complete framework for building web applications, with Akka running under the hood.
  • Shapeless pushes the boundaries of what’s possible in the Scala language itself.

My advice after learning Scala: dip your toes in at least one of them that looks interesting.

#10 Practice by Building Something You Would Use

Finally, I recommend building something. Don’t let that new knowledge go to waste. Even if you use just the plain Scala language with no other libraries, you should make something out of nothing.

What should you do?

My recommendation has always been: build something that you would use yourself. A web scraper, a file synchronizer, a Twitter bot, a chess engine, generative art, fractals, an ML pipeline, the number pi with 10 billion decimals, whatever looks interesting. Satisfy your curiosity with the new tool that you’ve discovered.

If no ideas come to mind, try contributing to the above projects. They’re all open-source and extra pairs of eyes and hands are always welcome.

Conclusion

So why should you learn Scala at all? Here are a few reasons:

  • It’s a rare skill and many companies are desperately searching for Scala developers. It has some of the best-paid programming positions.
  • It’s a giant leap for your mindset as a developer. Even if you don’t end up writing Scala (which is rare), you have some mental models that you can apply to any programming environment.
  • It’s just plain fun. Scala can be so expressive, so compact and yet so powerful. With some of the tools above, you can write in 20 lines what other developers need 2000 lines.

I hope the above ideas and tips were useful. Tens of thousands who learned from Rock the JVM have followed them, with some amazing results for their projects and their careers.

You might also like:

Authors

Daniel Ciocîrlan
Daniel Ciocîrlan

I'm a software engineer and the founder of Rock the JVM, where I teach everything in the Scala world. So far (september 2021) I've reached 43000 engineers in 163 countries, and I've personaly trained high-performing teams at Adobe, Apple, Orange and other top companies in Europe and the US.

Latest Blogposts

18.10.2021 / By Agata Nowak

Looking at a piece of the Functional World

Functional World is an internal event that Scalac created to exist as a place for the programming community and functional freaks who will communicate with people from all over the world. Functional World comes to the rescue in times of a pandemic –  the online conference formula is streamed on Twitch and YouTube. The first […]

14.10.2021 / By Agata Nowak

Meet Scalac At Web Summit 2021 in Lisbon!

This fall, prepare to meet Scalac at the best and largest annual technology conference worldwide – Web Summit 2021.  The organization is prestigious and professional on its own. Usually, this event doesn’t need much introduction. It’s practically everything startups wish to get involved with – an event that will let you grow your business, make […]

11.10.2021 / By Agata Nowak

In the center of Scalar

Scalar is one of the largest conferences in Central Europe. It started its activities in 2014. The Scalar community focuses on supporting people interested in the Scala programming language. It allows them to develop their programming skills, learning Scala, and delve into more and more theories with practical use. From 2014 to 2019, the conference was […]

Need a successful project?

Estimate project