Scala 3 Odyssey: Part 1 — Intro to Scala Programming Language

Antonel Ernest Pazargic
8 min readJun 11, 2024

--

Generated by OpenAI’s DALL-E

About myself

I am Antonel Ernest Pazargic, a JVM-based software developer working daily with Java and, more recently, Kotlin.
In regards to my exposure to functional programming, I might say that, a few years ago, around the time COVID hit hardly our world, I decided to start learning Functional Programming paradigm.
That was because I’ve noticed that back then it was getting traction in many main stream programming languages, such as Java and C#.
And in doing so I’ve started learning Haskell first and F# later on.
Needless to say that moment was the NO TURNING BACK POINT to me, and I’ve continued to learn more and more functional programming.
Despite the fact I’ve witness the power of Haskell as a pure Functional Programming language, I’ve found F# more pleasant because it is a lightweight Functional Programming language.

My experience with Scala

As I’ve already mentioned, considering my main experience was on JVM, I’ve decided to give scala a try.
It wasn’t the first time starting to learn and apply scala, but I remember that, back then, I was unpleasantly surprised of the backward incompatibility issues scala community was struggling with.
I’ve also remembered the big pain in those days was the poor support in the editors or IDEs (IntelliJ Idea).

The advent of Scala 3, and considering
- some great books about the Scala 3 (see references section below),
- the simplification and backward compatibility was considered seriously by the scala team,
drove me to consider resuming learning scala 3.
From the very beginning it was clear to me that, in the JVM world, Scala 3 is the most powerful programming language.
Moreover, for the last few years Visual Studio Code has become my first choice as development tool (editor).
And, all of the sudden, it, together with Scala (Metals) extension, became the first choice for authoring scala code.

About the article series

Having in mind that my main intention for next period is to become better in the realm of FP and Scala 3 programming language, and Scala ecosystem as a whole, I’ve decided to start this series about this beautiful and powerful programming language and share my finding, and hopefully joy, with you.

My primary objectives with this series are:

  • Having a good grasp of what Scala Programming Language is and enlist its main features, advantages and disadvantages,
  • How to install Scala 3 on your machine and setup the development tools,
  • Create a simple Scala application with the help of the Scala build tool (SBT) or scala-cli,
  • Dive into the Scala 3 programming language and its main features,
  • Get familiar with the Functional Programming paradigm and how to apply it in Scala 3,
  • Learn about the Functional Design Architecture,
  • Deep dive into the Scala 3 ecosystem and its main libraries and frameworks, such as Cats, ZIO, Akka or com.lihaoyi scala platforms,
  • And, why not, learn about the Scala 3 meta-programming features, monads, type classes,
  • The hot topic of the moment, Scala 3 effect system and direct style.

There is quite a huge ambition for this series, but I am confident this journey will be paved with joy and knowledge.
So, without further ado, let’s start our journey into the Scala 3 programming language.

Introduction to Scala

Scala programming language was designed by Martin Odersky and first released in 2003 at the École Polytechnique Fédérale de Lausanne (EPFL) in Switzerland.
Scala is a general-purpose programming language that is designed to express common programming patterns in a concise, elegant, and type-safe manner.
It is a statically typed language, which means that every expression’s type is known at compile time.
It smoothly mixes features of object-oriented and functional programming paradigms.
And it carefully picked up the useful features from pure functional programming languages (Haskell), like type classes and kinded type.
You will going to find out more about the scala 3 amazing features by following along these series articles.

Why Scala?

You might ask why scala and not keep with Java and Kotlin.
I admit that these two programming languages are very powerful and there is an amazingly astonishing communities around them, but, in my personal opinion, Scala has many other advantages that make it a popular choice for many developers.

Here there are some of the key advantages:

  • Functional Programming: Scala is a functional language, which means that functions are first-class citizens. Functions can be passed as arguments, returned from other functions, and assigned to variables.
  • Object-Oriented Programming: Scala is also an object-oriented language, which means that it supports classes, objects, and inheritance. And the mix between the FP and OOP makes it so appealing to that point to put at developer’s fingertips whatever options fits his/her needs.
  • Type Inference: Scala has a powerful type inference system that allows the programmer to omit certain type annotations. This makes the code more concise and readable.
  • Expressive/concise Syntax: Scala has a concise and expressive syntax that allows the programmer to write code that is easy to read and understand. Almost everything is an expression, if-else, pattern match, for- comprehension…
  • Pattern Matching: Scala has a powerful pattern matching system (one of the most powerful I came across with) that allows the programmer to match complex data structures.
  • Immutable Data Structures: Scala encourages the use of immutable data structures, which can help prevent bugs and make the code easier to reason about.
  • Lazy Evaluation: Scala supports lazy evaluation, which means that expressions are only evaluated when they are needed.
  • Concurrency: Scala has built-in support for concurrent programming, with features such as scala composable futures.
  • Interoperability: Scala is fully interoperable with Java, which means that Scala code can call Java libraries and vice versa.
  • High-Level Abstractions: Scala has many high-level abstractions that make it easy to express complex ideas in a concise way.
  • Scalability: Scala is designed to be scalable, which means that it can be used to write small scripts or large applications.
  • Meta-programming: Scala has powerful meta-programming features that allow the programmer to generate code at compile time.

Disadvantages of Scala

  • Steep Learning Curve: Scala has a steep learning curve, especially for developers who are new to functional programming.
  • Complexity: Scala is a complex language with many features, which can make it difficult to understand and maintain. It is seen by many as a language that is hard to master, that offers many ways to do the same thing (shoot yourself in the foot).
  • Tooling: Scala tooling is not as mature as for Java or Kotlin, which can make it difficult to set up a development environment.
  • Compatibility: Scala has had issues with backward compatibility in the past, which can make it difficult to upgrade to new versions of the language.
  • Compilation Time: Scala has a longer compilation time compared to Java or Kotlin, which can slow down the development process.
  • Community: Scala has a smaller community compared to Java or Kotlin, which can make it difficult to find help or resources.
  • Documentation: Scala documentation is not as comprehensive as Java or Kotlin, which can make it difficult to learn the language.

Because my main goal it learning Scala 3, I can’t go to the next section without mentioning that some disadvantages of the Scala 2.x were addressed in Scala 3, such as the backward compatibility, the complexity, the compilation time, the documentation, and, one of my favorite, the tooling.

What types of applications can you build with Scala?

Scala is a versatile language that can be used to build a wide variety of applications, including:

  • Web Applications, by using frameworks like Play or Akka HTTP
  • Data Processing Applications, by using libraries like Spark or Flink
  • Distributed Async Systems, by using libraries like Akka
  • Machine Learning Applications, by using libraries like Smile
  • Mobile Applications, by using libraries like Scala.js
  • Desktop Applications, by using libraries like ScalaFX
  • Native Applications, by using libraries like Scala Native or GraalVM
  • CLI applications, by using libraries like scala-cli, Ammonite and Typelevel Decline, or com.lihaoyi platform (MainArgs)

Scala is used by many companies around the world, including:

  • Twitter
  • LinkedIn
  • Xebia
  • The Guardian
  • Sony
  • ING

Scala ecosystem

The community around Scala is very active and there are many companies that contribute to the Scala ecosystem, including:

  • Scala Center
  • Scalameta — which develops the Metals, a implementation of the LSP for Scala
  • Virtuslab — which rise the bars to the next level for the REPL (interactive scala) by the creation of scala-cli
  • Typelevel — which develops the Cats libraries
  • Ziverge — the company behind the ZIO libraries
  • Lightbend — which offers the Akka libraries
  • JetBrains — the company behind the well-known IntelliJ IDEA (including Scala Plugin)

A sneak peek into a scala powerful features

Up until now I’ve described various aspects about Scala programming language, but I haven’t shown you any code.
Let’s remediate this by showing you a simple example of how to use Scala’s powerful features.

Having the following extension declaration

its usage might looks like below

In just a few lines of code I’ve used quite some powerful features of Scala:

  • extension methods
  • type classes
  • context bounds
  • using clause

You can ran this piece of code in the online scala playground (scastie)

https://scastie.scala-lang.org/qEA3qM9HTXall1ZyNxMJvw

References

  • A summary of all my articles in this mini-series
  • Programming in Scala, 5rd Edition by Martin Odersky, Lex Spoon, and Bill Venners and Frank Sommers
  • Advanced Programming in Scala, 5nd Edition by Martin Odersky, Lex Spoon, and Bill Venners
  • Rock the JVM has some great videos on Learning Scala
  • This week in scala

Final thoughts

This ends my first article of the Scala Odyssey series.
Keep an eye on my next articles where I will show you how to install Scala 3 on your machine, setup up the development tools, and create a simple Scala application with the help of the Scala build tool (SBT) or/and scala-cli.

If you have any questions or suggestions, please feel free to leave a comment below.

In case you enjoyed this article as much as I did writing it, please clap for it, and share it with your friends and colleagues, or whoever you think might benefit from it.

You can also follow me on Twitter at (https://twitter.com/pazargic9506) for more updates.

Many thanks

--

--