Scala is a programming language developed by Martin Odersky, a computer scientist and professor of programming methods at École Polytechnique Fédérale de Lausanne in Switzerland. The language was designed to incorporate both object-oriented and functional programming paradigms. It is a strong statically typed language which strives to provide strong expressivity and runs on the JVM (Java Virtual Machine).
In its design, Scala provides language interoperability with Java to enable developers to leverage the existing java ecosystem. This does not imply that Scala has no rich ecosystem. On the contrary, there exist a lot of tools, frameworks and libraries for Scala ranging from big data processing frameworks (like Apache Spark) to full stack web development frameworks (like Play). Since Scala’s first public release in 2003 the language has been continuously improved and reached 2022 its third major release (Scala 3).
The new overhauled Scala 3
In the newest major release (Scala 3.x ) substantial new changes were made that improve and ease working with the language. Its type inference was improved and new features like Opaque Types, Intersection and union types, dependent function types, polymorphic function types, match types, enumerations and more were included just to name a few.
Additionally, there were also improvements in the object-oriented part of the language. Just to mention a few of the new features. One new feature is Open classes. One problem in object-oriented programming occurs if classes are extended that are not designed for extension. In Scala 3 classes that are explicitly written to be extended the open soft modifier should be used. It’s simple but helps to keep track of which classes can be extended and which should not. Another new feature that further supports Composition over inheritance is the export clause feature. Export clauses define aliases for members of an object to ease composition.
The last update in Scala 3 ships new powerful tools for metaprogramming. Especially, the new inline feature optimizes performance.
After jumping directly to the new features and getting a glimpse of the changes in Scala 3 let us jump back to the start and introduce Scala 3.
Back to the roots: ‘Hello World!’
First to install Scala please follow the installation instruction at Scala’s getting started section.
In Scala 3 a Hello World program can be written like:
@main def hello() = println("Hello, world!")
Let us decompose the code snippet to get a better understanding. The keyword def defines a function. In our specific case the function name is hello and contains no parameters. After the closing brackets of the parameters, a shorthand notation is used. Usually,
def hello() = { println("Hello, world!") }
would require curly brackets after the equal sign, but you can omit them if you only have one expression (which is the println(…) call). The last command println(…) prints hello world after the hello() function is called. New in Scala 3 is the @main which helps identify which function is the main function. In Scala 2 this hello world snippet was more verbose and a special Trait named App was needed:
object Hello2 extends App { println("Hello, world") }
The first code snippet can be compiled with a tool scalac (similar to javac). Let’s assume you wrote the code @main def hello() = println(“Hello, world!”) in a file called HelloWorld.scala. You can now compile the file with the following command in the terminal:
scalac HelloWorld.scala
This will generate the known .class bytecode files that can be run in the JVM. The Scala compiler also generates files ending by .tasty. TASTy is a high-level interchange format for Scala 3 which stands for Typed abstract Syntax Trees and represents all your source code. This format helps to tackle the type erasure problem of .class files. If you generate a simple List of Integers for example in Scala:
val myIntList: List[Int] = List(1, 2, 3)
The resulting .class file would end up looking like this (this can be done with the javap command):
public scala.collection.immutable.List myIntList();
As one can see the List lost its Integer type. TASTy enables a separate compilation, and recompilation for different JVM versions by just compiling first the scala code into tasty and afterwards into .class bytecode format. The TASTy helps to keep all the code information without the need for the original source code. If a compilation of different JVM versions are needed the TASTy format can be used to recompile them.
What tools are available for Scala?
This is a non-comprehensive list of Scala Tools and material that are interesting and helpful.
Coursier: A Scala application and artefact manager that helps to set up your Scala development environment
Ammonite: Helps using Scala as a script or in a REPL (Read-Eval-Print-Loop)
sbt: I a simple build tool (sbt) for Scala and Java.
Scala-doc: The Scala documentation contains general guides, migration guides, language reference and more information.
Metals: A scala language server with rich IDE features. You can even try it online.
How can I learn more?
This article is a part of a greater series centred around the technologies and themes found within the first edition of the TechRadar by Devoteam. To read further into these topics, please download the TechRadar by Devoteam .
Want to learn more about Scala?
Check out TechRadar by Devoteam to see what our experts say about its viability in the market.