Scala 3, the latest version of the object-oriented, functional language with JVM and JavaScript runtimes, is now available as a production release. The upgrade, described by the project as a “complete overhaul,” features a new type system and syntax improvements.
The production release of Scala 3 was introduced May 14, following a release candidate stage that began on February 17. Among the improvements noted in the release is a new “quiet” syntax for control structures such as if, while, and for.
[ Also on InfoWorld: JDK 17: The new features in Java 17 ]
Scala has both JVM and JavaScript runtimes and is positioned for use in building high-performance systems with access to ecosystems of libraries. Instructions for downloading Scala 3 can be found here.
Other syntax improvements in Scala 3:
- Optional braces that support a distraction-free, indentation-sensitive style of programming.
- The
new
keyword is now optional. - Type-level wildcards have been changed from
_
to?
. - Implicits and their syntax have been heavily revised.
New language features in Scala 3:
- In an improvement over Scala 2 implicits, using clauses can be specified by type, freeing function signatures from term variable names that are never explicitly referred to.
- Given instances allow programmers to determine the canonical value of a certain type, making programming with type-classes more straightforward, without leaking implementation details.
- Extension methods are now directly built into the language, for better error messages and improved type inference.
- Implicit conversions have been redesigned as instances of a type-class
Conversion
. - A context functions feature makes contextual abstractions a first-class citizen. These serve as a tool for library authors to express concise domain-specific languages.
- If an implicit parameter cannot be resolved by the compiler, import suggestions are provided that may fix the problem.
Type system improvements in Scala 3:
- Enums, or enumerations, have been redesigned to blend well with case classes and form the new standard to express algebraic data types.
- Opaque type aliases enable developers to hide implementation details without taking a performance hit.
- Intersection and union types enable expression of type constraints outside the inheritance hierarchy.
- Polymorphic function types can abstract over functions that take type arguments in addition to their value arguments.
- Type lambdas are type-level functions that can be passed as type arguments without needing an auxiliary type definition.
- Rather than encoding type-level computation using implicit resolution, Scala 3 supports matching on types.
Object-oriented programming features in Scala 3:
- Traits move closer to classes and now can take parameters, thus making them more powerful as a tool for modular software decomposition.
- Open classes require library designers to mark classes as open.
- Utility traits that implement behavior sometimes ought not be part of inferred types. With Scala 3, those traits can be marked as transparent, hiding the inheritance from the user.
- Explicit null moves null out of the type hierarchy, for catching errors statically. Additional checks for safe initialization find access to unitialized objects.
Scala 3 also offers tools for metaprogramming, including compile-time operations, quoted code blocks, and an inline feature that allows values and methods to be reduced at compile time.