Scala release 3.0 brings a number of enhancements & new features.

The upcoming and exciting new version of Scala 3 brings many improvements and new features. Here, we provide you with a quick overview of the most important changes. There are some resources available if you want to dig deeper.

  • The Scala 3 Book: For developers new to The Scala language.
  • Syntax summary: provides you with a formal description of the new syntax.
  • Language Reference: Describes in detail the changes from Scala 2 to Scala 3.
  • Migration Guide: Gives you all the information necessary to migrate from Scala 2 to Scala 3.

New in Scala 3

Scala 3 is a complete redesign of the Scala language. At its core, many aspects of the type system have been changed to become more principled. While this also brings exciting new features (like federated typing), it first means that the type system (even) gets in your way a lot less, such as type reasoning and overload resolution, which have been greatly improved.

Grammar to improve

In addition to many (minor) cleanups, the Scala 3 syntax offers the following improvements.

  • Provides a new “quiet” syntax for control structures, e.gif.while, andfor New control syntax
  • newThe keyword is optional _ (also known as _)Creator application).
  • Optional curly braces to support a non-intrusive, indent sensitive programming style
  • willType level wildcardfrom_Instead of?
  • Implicits (and their syntax) have been extensively modified.

Context abstraction

One of Scala’s basic core concepts was (and still is, to some extent) to provide users with a small set of powerful capabilities that can be combined into huge (and sometimes unpredictable) expressive capabilities. For example, implied features are used to simulate context abstraction, express type-level computation, simulate type classes, perform implicit coercion, code extension methods, and so on. Learning from these use cases, Scala 3 takes a slightly different approach, focusing on intent rather than mechanism. Scala 3 does not offer one very powerful feature, but several custom language features that allow programmers to express their intentions directly.

  • Reduction in context information. The use of clauses allows the programmer to abstract the information available in the call context, which should be passed implicitly. As an improvement on Scala 2’s implicit information, use clauses that can be specified by type, freeing function signatures from the never explicitly mentioned terminology variable names.

  • Provide type class instances. A given instance allows a programmer to define a certain type of _ typical value _. This makes programming with type classes more straightforward without giving away implementation details.

  • Extend classes retroactively. In Scala 2, extension methods must be encoded using implicit transforms or implicit classes. In Scala 3, by contrast, extension methods are now built directly into the language, resulting in better error messages and improved type reasoning.

  • Treat one type as another. Implicit conversions have been redesigned to be instance conversions of type classes.

  • Higher level context abstraction. The _ new _ feature of context functions makes context abstraction a first-class citizen. They are an important tool for library authors, allowing concise expression of domain-specific languages.

  • Actionable feedback from the compiler. If an implicit argument cannot be resolved by the compiler, it now provides import suggestions that might resolve the problem.

Type system improvement

In addition to vastly improved type reasoning, Scala 3’s type system offers many new features, giving you powerful tools to statically express invariants in types.

  • Enumeration. Enumerations have been redesigned to blend well with case classes and form a new standard for expressing algebraic data types.

  • Opaque type. Implementation details hidden behind opaque type aliases at no cost to its performance! Opaque types replace value classes and allow you to create an abstract barrier without incurring additional boxing overhead.

  • Cross and union types. Putting the type system on A new footing led to the introduction of A new type system feature: instances of intersecting types, such as A & B, are instances of A and B. Joint type instances, such as A | B, is an instance of A or B _. These two structures allow programmers the flexibility to express type constraints outside of the inheritance hierarchy.

  • Dependent function type. Scala 2 already allows return types to depend on (value) parameters. In Scala 3, this pattern can now be abstracted to express dependent function types. In type F = (e: Entry) => e.key, the result type _ depends on the _ parameter

  • The function type of polymorphism. As with dependent function types, Scala 2 supports methods that allow type parameters, but don’t allow programmers to abstract those methods. In Scala 3, polymorphic function types (such as [A] => List[A] => List[A]) can abstract functions that take _ type _ arguments in addition to value arguments.

  • The Type of lambdas. What was required in Scala 2 to be expressed by compiler plug-ins is now a first-class feature in Scala 3: Type lambdas are type-level functions that can be passed as Type parameters of (higher types) without the need for auxiliary Type definitions.

  • Match type. Scala 3 provides direct support for type matching, rather than using implicit parsing to code type-level computation. Integrating type-level computing into the type checker improves error messages and eliminates the need for complex coding.

Object-oriented programming was redesigned

Scala has always been at the forefront of functional and object-oriented programming — and Scala 3 pushes the boundaries in both directions! The above changes to the type system and the redesign of context abstractions make _ functional programming _ easier than ever. At the same time, the following new features enable well-structured object-oriented design and support best practices.

  • Inheritance. Attributes are closer to classes, and parameters can now be accepted, making them more powerful as tools for modularizing software decomposition.
  • Plan for expansion. Extending classes that are not intended to be extended is a long-standing problem in object-oriented design. To solve this problem, open classes require library designers to explicitly mark classes as open.
  • Hide implementation details. The practical characteristics of the implementation behavior should sometimes not be part of the inferred type. In Scala 3, these attributes can be marked as transparent, hiding inheritance from the user (in inferred types).
  • Composition trumps inheritance. This phrase is often quoted, but is cumbersome to implement. Scala 3’s export clause does not: it is symmetric with the import clause, which allows the user to define aliases for selected members of the object.
  • There will be no NPEs. Scala 3 is more secure than ever:Explicit crosstabwillnull, to help you statically catch errors;Safe initializationAdditional checks can detect access to uninitialized objects.

metaprogramming

While macros in Scala 2 are an experimental feature, Scala 3 provides a powerful tool library for metaprogramming. Detailed information about the different facilities is included in the macro tutorial. In particular, Scala 3 provides the following capabilities for metaprogramming.

  • Inline. As a basic starting point, the inline functionality allows you to reduce values and methods at compile time. This simple feature already covers many uses and provides a gateway to more advanced functionality.
  • Compile-time operation. The package scala.com PileTime contains additional functionality that can be used to implement inline methods.
  • Reference block. Scala 3 adds codeAccurate quotationProvides a convenient high-level interface for building and analyzing code. Building plus one plus code is like'{1 + 1}As simple as that.
  • The reflection API. For more advanced use cases, quotes.Reflect provides more detailed control over checking and generating program trees.

If you want to learn more about metaprogramming in Scala 3, we invite you to attend our tutorial.