DEV Community

Pedro H Goncalves
Pedro H Goncalves

Posted on • Updated on

A little about the Scala Language

What is Scala?

Scala is a compiled multiparadigm language that runs on the JVM. It was inspired by Java, Erlang, and Haskell. It has a static type system with first-class type inference, making it one of the "modern" languages with a sophisticated type system. By saying that Scala is multiparadigm, we emphasize that the language embraces different programming styles. Unlike other languages that tend to favor a specific paradigm, Scala gives developers the freedom to choose the programming style that best suits the problem at hand. Additionally, the language is committed to code conciseness and expressiveness. Surprisingly, Scala manages to deliver all these features. In this article, I will explain how.

Functional Programming in Scala
Scala has many functional programming features, such as first-class functions. First-class functions allow functions to be treated as values, assignable to variables, returned by other functions, and passed as parameters. This provides a high level of abstraction and modularity, especially for data manipulation. Functional programming in a static type system can be challenging, but Scala offers features that facilitate this task. Additionally, Scala supports lambda expressions, allowing the creation of anonymous functions in a concise and expressive way. Immutability is encouraged through the val and var keywords for declaring immutable and mutable variables, respectively. Immutability is useful for asynchronous and concurrent programming, topics that we will address later.

Object-Oriented Programming and JVM Interoperability
As mentioned earlier, Scala runs on the JVM and has seamless integration with Java as one of its main philosophies. It is possible to use Java libraries in Scala and vice versa. Interoperability between the two languages is simpler when it comes to basic data structures but can become more complex when dealing with more advanced data structures due to subtle differences in Java and Scala syntax. Scala incorporates important concepts of object-oriented programming, such as class and object creation, encapsulation, and useful abstractions like abstract, case, and final class, which simplify the declaration of operations. Inheritance and polymorphism are also supported by the language, allowing for more flexible manipulation of types according to the application context.

All these data manipulation and new type creation features allow developers, even with a static type system, to have the freedom to define the desired behavior for their solution, avoiding type errors and facilitating the creation of comprehensive tests. Additionally, first-class type inference reduces the need for explicit type declarations, giving the language an appearance of dynamic typing while maintaining the safety of the static type system.

Scalability
Building upon what was mentioned earlier, the concept of immutability present in the language enables concurrent programming using multiple threads, resulting in better performance and the construction of more robust and efficient solutions. Alongside immutability and concurrent programming, Scala offers support for asynchronous tasks that execute work differently but leverage many of the benefits of concurrent programming. Furthermore, Scala is known as the foundation of two famous frameworks: Spark and Akka.

Spark
Spark is widely used for distributed processing and data analysis. It has APIs in multiple languages such as Python, R, and Java, and is widely adopted in both academic and commercial environments. The Spark MLlib library is dedicated to machine learning and data visualization.

Akka
Akka is a library used for creating distributed and concurrent systems. It implements the actor model, an abstraction for concurrent programming based on message passing. The Akka ecosystem includes Akka HTTP for web application development, Akka Streams for stream processing, and Akka Cluster for handling actor clustering in distributed environments.

Web Development
For web development in Scala, there are two popular options. The first one is the Play Framework, which follows the MVC (Model-View-Controller) pattern and is similar to other popular frameworks such as Rails, Django, and Laravel. The Play Framework provides development abstractions that make web application creation quite enjoyable. The second option is Akka HTTP, which offers greater scalability but requires knowledge of actors and asynchronous message passing. Although it may require a bit more experience, Akka HTTP is powerful and flexible. Additionally, there is Slick, which is a commonly used ORM/FRM in Scala web development. Slick provides efficient abstractions over JDBC, including support for result streaming. While Slick doesn't have built-in object migrations, it can be combined with tools like Flyway, which integrates well with Slick and the Play Framework.

Weaknesses
Initially, Scala was developed with an academic focus, and this emphasis can be observed in some parts of the documentation, where complex concepts are presented without proper prior explanation. An example of this is the creation of the Cats library, which provides abstractions of the functional paradigm to fill some gaps in the official documentation. This approach ended up alienating some people who preferred to start their studies in another language. Additionally, the Scala community has faced some issues that negatively affected the perception of the language. Controversies involving creators and maintainers of famous libraries are an example of this. We can also attribute some blame to the creation of Scala 3, which became a "distraction" since Scala 2 still receives constant updates and many people are not focused or at least interested in making a (often laborious) migration from Scala 2 to Scala 3. The already "small" community around the language, which has experienced reductions, has become "elitist," where only big tech companies (LinkedIn, Netflix, Twitter, Airbnb, Spotify) use its technologies since skilled professionals and experienced architects of distributed systems (its main application) have become increasingly expensive. As a consequence of the "elitization" of the language, job opportunities have been consistently scarce, which doesn't usually attract new learners. All these events have led to the loss of popularity of the language, but it will undoubtedly continue to be widely used in commercial settings.

Conclusion
Despite these weaknesses, Scala remains a powerful and widely used language in many environments. Its combination of object-oriented and functional programming, interoperability with Java, and scalability features make it a solid choice for a variety of applications.

After discussing Scala and its (dis)success to some extent, I encourage everyone to write a few lines in the language. Despite criticism of the documentation, there is a significant amount of introductory content available on the internet, and I recommend the Rock The JVM channel for those interested in the language.

Thank you very much for reading.

Top comments (2)

Collapse
 
krlz profile image
krlz • Edited

Hi, thanks for sharing, we currently use Scala in the company I am working on, it has a great syntax, we didn't really move to cats style , but we take focus on making pure functions and avoiding inmutability, this is already for us a big plus, the code tends to be more organize and clear, and whenever we need we can still use JAVA libraries

Collapse
 
pedrohgoncalves profile image
Pedro H Goncalves

its always really nice to know that other companies use scala. Funcional programming have some features for collaborate in organized and clean code, i recommend you to learn the deep