Scala 3 Hello, world!
The final version of Scala 3, the single largest revision to language in more than a decade, is here, and we’re more than excited!
@main def hello = println("Hello, world!") $ scala Hello.scala
Scala offers a lot in terms of creating more complex abstractions that come with the functional aspect of the language. By promoting immutability and other functional programming concepts, it naturally encourages developers to build systems that scale easily, even in a distributed environment, and have a clean and well-organized architecture.
The business likes Scala as well. It is a prime language for tinkering with data and getting actionable insights. The event sourcing model, which is closely tied with immutability and functional programming, is a natural fit for many tech companies. Giants like Apple, Linkedin, Morgan Stanley, and Netflix are some of Scala prominent adopters and use Scala to save resources and model difficult business domains precisely.
Demand for Scala programmers is growing and now they have an even better tool at their disposal. Let’s find out more about Scala 3!
What’s gone in Scala 3
The new update makes Scala more user-friendly and removes some complexities from the syntax. It introduces a multitude of changes and promises to make Scala more usable than ever. In this blog post, Mikołaj Koziarkiewicz has subjectively selected several language features in Scala 2 and explored the equivalent features of Scala 3. He talks about ambiguous underscores, implicits, and enums.
There are also notable features being removed in Scala 3. Here are some of them, highlighted by our Senior Software Engineer — Jacek Kunicki:
- Scala 2 macros
- symbol literals (but the
Symbol
class stays there for backwards-compatibility) - procedure syntax (you can’t write
def f() { ... }
anymore without at least the=
sign) - auto-application (now you must use
()
when calling a method with no parameters) - the
do
-while
loop (thedo
keyword has a new meaning — see the new control syntax - existential types with
forSome
- limit of 22 parameters in a function and 22 fields in a tuple
There are also a couple of deprecated features — they’re still there for now, but will be removed in future releases. Among those, you can find:
- package objects — to be replaced by top-level definitions
private[this]
andprotected[this]
access modifiers — the former would be inferred by the compiler when you use justprivate
, the latter is going to be dropped- XML literals — to be eventually replaced with XML string interpolation
- wildcard initializer, i.e.
var x: A = _
— it’s still there to support cross-compilation, but you should now be usingscala.compiletime.uninitialized
instead
What’s new in Scala 3
Back in March, we dived deep into pre-release and picked the things we really like in Scala 3. Here are short video presentations and coding-sessions where we play with Scala 3 code and features.
Context is King
Context functions are some of the new additions to Scala 3. They allow interaction between two other Scala features: functions-as-values and implicit (given) parameters. How to use this construct? What might be the potential use-cases? Watch the video and read this blog post by Adam Warski.
Opaque types
Opaque types are a new tool to the Scala Developer modelling toolbox. Types are a programmer's best friend. In the pursuit of a perfect world, we try to model our domains in such a way that the type system makes incorrect data impossible. In Scala 2, we have tools like type aliases, value classes, or Refined Types. In Scala 3, there's something new for this purpose — opaque type aliases. Magda Stożek explains what they bring to the table and how they can make developers' lives easier.
Contextual abstractions
Implicits are, at the same time, probably some of the most hated and the most used features in Scala. They are incredibly useful if you know what you’re doing, but incredibly frustrating when you don’t. For developers new to Scala, implicits are often arcane enough to even discourage them from using the language — especially when they realize that “implicit” can mean at least half a dozen different things. It’s no wonder, then, that implicits were the subject of a considerable overhaul of Scala 3. Watch Mikołaj Koziarkiewicz’s talk about what has changed in this matter with the introduction of Scala 3.
Other useful changes in Scala 3
- simplified syntax — braces are now optional, now blocks of code can be delimited by indentation only
- new syntax for enums (ADT-like, with parameters, proper pattern matching and
java.lang.Enum
compatibility) - the
new
keyword is now optional — even non-case classes now have an auto-generatedapply
- union and intersection types (
A & B
andA | B
) - top-level definitions — which are meant to replace package objects
- principled metaprogramming that includes macros: compile-time code generation
Practical information for developers
Scala Center
The Scala Team at @scala_lang has prepared a guide for developers new to Scala — Scala 3 Book and some detailed materials that help everyone get familiar with the new version: Migration Guise, Syntax Summary, and Language Reference. The official announcement of what’s new in Scala 3 is posted here. And here, you can find the community build.
Try Scala 3 here.
Scala Times
The Scala Times newsletter celebrated the release with a Special Issue with Scala-3-related links from previous ScalaTimes issues, introducing new features or detailing out migration paths.
Read the Scala 3 Special Issue here.
Keep in mind this is only the tip of the iceberg, you can find much more in the Scala Times archives. Subscribe to Scala Times to be up to the with the Scala news!
Scala 3 Tech Report
A few months ago, we ran the Scala 3 Developers’ Survey and published the results shortly before the Scala largest revision release.
The biggest hope for Scala 3 is centered around the simplification of the language. According to developers, simplification of the language can take many forms:
- people expect code to be simpler, due to simpler syntax and removing some of the Scala 2 warts.
- people hope that the language will be easier to learn, explain, and introduce to teams that are not yet experienced with Scala or functional programming.
To get a better understanding of how developers feel about this major change in the Scala language, download the full version of the Scala Tech Report here.
In need of Scala Developers?
We love using Scala to build efficient and scalable software products. Our clients usually approach us with unstable, slow-running systems with scalability issues.
Do you need strong technical experts who will fix, write code from scratch, or consult existing solutions and help to refine them? Let’s talk