Scala 3-Features

·

4 min read

scala 3 main features

Scala has a fairly expressive type system with type inferencing. get the benefit of both worlds - you can do statically checked duck typing with Scala.

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 now _ to ?. Implicits and their syntax have been revised. New language features in Scala 3:
implicits, using clauses can be specified by type,

Given instances allow programmers to determine the canonical value of a certain type, making programming with type-classes more straightforward,
Extension methods are become inbuilt feature, display 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, so now library authors can express concise domain-specific languages.

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. Intersection and union types enable the expression of type constraints outside the inheritance hierarchy. Polymorphic function types can abstract over functions that take type arguments along with value arguments. Type lambdas are type-level functions that can be passed as type arguments without needing an auxiliary type definition.

Traits become similar to classes and also can accept parameters, . Open classes require 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. Inline. the inline feature allows values and methods to be reduced at compile time.
Compile-time operations. The package scala.compiletime contains additional functionality that can be used to implement inline methods.
Quoted code blocks. Scala 3 adds the new feature of quasi-quotation for code, providing a convenient high-level interface to construct and analyse code. Constructing code for adding one and one is as easy as '{ 1 + 1 }.
Reflection API. For more advanced use cases quotes.reflect provides more detailed control to inspect and generate program trees.
Abstracting over contextual information. Using clauses allow programmers to abstract over information that is available in the calling context and should be passed implicitly. .
Providing Type-class instances. Given instances allow programmers to define the canonical value of a certain type. This makes programming with type-classes more straightforward without losing implementation details.

Viewing one type as another. Implicit conversions have been redesigned from the ground up as instances of a type-class Conversion. Higher-order contextual abstractions. scala 3 now suggest Actionable feedback from the compiler. In case an implicit parameter cannot be resolved by the compiler, it now provides import suggestions that may fix the problem.

Parameterized enums Enums can be parameterized.

The values of an enum correspond to unique integers. The integer associated with an enum value is returned by its ordinal method:

scala> val green = Color.Green        


val red: Color = Green        


scala> green.ordinal       


val res0: Int = 0

scala3_enum.png

Opaque Types. Hide implementation details behind opaque type aliases without paying for it in performance
Intersection and union types. Basing the type system on new foundations led to the introduction of new type system features: instances of intersection types, like A & B, are instances of both A and of B. Instances of union types, like A | B, are instances of either A or B.
Both constructs allow programmers to flexibly express type constraints outside the inheritance hierarchy.
Match types. Instead of encoding type-level computation using implicit resolution, Scala 3 offers direct support for matching on types. Integrating type-level computation into the type checker enables improved error messages and removes the need for complicated encoding.