Around IT in 256 seconds

#94: Scala: language with academic background and huge industry adoption

January 16, 2023 | 3 Minute Read

Scala is a programming language running on the Java Virtual Machine. It’s statically typed, and you can use it both as functional and object-oriented language. Even at the same time. The functional side of Scala supports higher-order (and higher-kinded) types. For those of you know what it means. On the other hand, object-relational features of Scala are equally strong. Including powerful trait composition, that you can somewhat compare to multiple inheritance. Combine than with great Java interoperability and no wonder why Scala became a go to language for many ex-Java developers.

Let’s start from the surface: the syntax. Scala somewhat resembles Java. But there are many shortcuts and things you can skip to make it more concise, for example:

  • optional semicolons
  • optional return statement
  • optional block braces (indentation is significant, like in Python)
  • optional type declarations
  • optional parens and dot when invoking methods (so-called point-free style)

And so on, and so forth. We don’t have time to cover all of these, so let’s take the last one as an example. In Scala, you can say 1 to 10 to generate a range of numbers. Like, literally, no punctuation whatsoever. You can simply say list = 1 to 10. In reality, it’s actually a method named to() invoked on an integer 1 with an argument 10. So you can use more verbose syntax Oh, and the method to() is actually an extension function. This means you can add new methods to existing classes. Even the ones you have no source code for. Scala has them as well.

As you can see, the syntax can be confusing, and it’s just the tip of the iceberg. But let’s not focus on the surface. If you quitely love Haskell but program in Java to make a living, Scala is for you. And I’m not only talking about very powerful type system. Scala support algebraic data types, currying, higher order functions, partial application, lazy evaluation, immutability… Each of these deserve a separate episode. You can even take advantage of tail call optimization. In short, it means that carefully implemented recursion will not blow away your stack with StackOverflowError.

Keep in mind that all of these features were available way before Java even had lambda expressions and records. And almost a decade before Kotlin. The latter often referred to as more powerful than Java, but simpler than Scala. In a sense, Scala was way ahead of the competition in terms of language features.

The industry adoption is pretty big and there are many killer apps and libraries. Probably the best known is Apache Spark, the successor to Hadoop. Also, Kafka is powered by Scala. That’s right, Scala is probably best known in distributed, high volume and high load industry. There are plenty of libraries to support high-performance concurrency. ZIO, Monix, Cats-effect, Scalaz - just to name a few.

With all these amazing features, many developers aren’t that positive about Scala. Many advanced language features like implicits, extension functions and macros make it hard to read. For years, even IDEs struggled with code completion. This also means that the compiler is quite slow. Last but not least, libraries compiled against one version of Scala need to be recompiled for other versions. Something unheard of in the Java community. That being said, it’s a powerful language. But with great power comes great responsibility.

That’s it, thanks for listening, bye!

More materials

Tags: Akka, JVM, Scala, Spark, functional-programming

Be the first to listen to new episodes!

To get exclusive content: