|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Overview" |
| 4 | +--- |
| 5 | + |
| 6 | +This section gives an overview of the most important language additions in Scala 3. |
| 7 | +The new features all address one or more of four major concerns: |
| 8 | + |
| 9 | + - [Consistency](consistency) |
| 10 | + - [Safety](safety) |
| 11 | + - [Ergonomics](ergonomics) |
| 12 | + - [Performance](performance) |
| 13 | + |
| 14 | +Scala 3 also drops a number of features that were used rarely, or where experience showed |
| 15 | +that they tended to cause problems in codebases. These are [listed in a separate section](./dropped). |
| 16 | + |
| 17 | +Not included in this overview are changes to meta programming and generic programming. So far these have relied on a macro system that had experimental status (`scala.reflect` macros. This macro system will be replaced with a different solution. The current state of the design will be described elsewhere. |
| 18 | + |
| 19 | +<a name="consistency"></a> |
| 20 | +## Consistency |
| 21 | + |
| 22 | +Constructs that make the language more consistent internally, and consistent with its foundations. Constructs that admit useful laws of reasoning. |
| 23 | + |
| 24 | + - Intersection type `A & B` |
| 25 | + |
| 26 | + Replaces compound type `A with B` (the old syntax is kept for the moment but will |
| 27 | + be deprecated in the future). Intersection types are one of the core features of DOT. They |
| 28 | + are commutative: `A & B` and `B & A` represent the same type. |
| 29 | + |
| 30 | + - Implicit function type `implicit A => B`. |
| 31 | + |
| 32 | + Methods and lambdas can have implicit parameters, so it's natural to extend the |
| 33 | + same property to function types. Implicit function types help ergonomics nnd performance |
| 34 | + as well. They can replace many uses of monads at an order of magnitude improvement |
| 35 | + in runtime speed, and with far better composability. |
| 36 | + |
| 37 | + - Dependent function type `(x: T) => x.S`. |
| 38 | + |
| 39 | + The result type of a method can refer to its parmeters. We now extend the same capability |
| 40 | + to the result type of a function. |
| 41 | + |
| 42 | + - Type lambda `[X] => C[X]` |
| 43 | + |
| 44 | + Type lambdas were encoded previously in an extremely roundabout way, exploiting |
| 45 | + loopholes in Scala's type system which made it Turing complete and unsound. With |
| 46 | + the removal of unrestricted type projection the loopholes are eliminated, so the |
| 47 | + previous encodings are no longer expressible. Type lambdas in the language provide |
| 48 | + a safe andmore ergonimic alternative. |
| 49 | + |
| 50 | +<a name="safety"></a> |
| 51 | +## Safety |
| 52 | + |
| 53 | +Constructs that help precise, typecheched domain modeling and that improve the |
| 54 | +reliability of refactorings. |
| 55 | + |
| 56 | + - Union types `A | B` |
| 57 | + |
| 58 | + Union types gives fine-grained control over the possible values of a type. |
| 59 | + A union type `A | B` states that a value can be an `A` or a `B` without having |
| 60 | + to widen to a common supertype of `A` and `B`. Union types thus enable more |
| 61 | + precise domain modeling. They are also very useful for interoperating with |
| 62 | + Javascript libraries and JSON protocols. |
| 63 | + |
| 64 | + - Multiversal Equality |
| 65 | + |
| 66 | + Multiversal equality is an opt in way to check that comparisons using `==` and |
| 67 | + `!=` only apply to compatible types. It thus closes the biggest remaining hurdle |
| 68 | + to type-based refactoring. Normally, one would wish that one could change the type |
| 69 | + of some value or operation in a large codebase, fix all type errors, and obtain |
| 70 | + at the end a working program. But universal equality `==` works for all types. |
| 71 | + So what should conceptually be a type error would not be reported and |
| 72 | + runtime behavior would change instead. Multiversal equality closes that loophole. |
| 73 | + |
| 74 | + - Null safety |
| 75 | + |
| 76 | + (Planned) Adding a `null` value to every type has been called a "Billion Dollar Mistake" |
| 77 | + by its creator, Tony Hoare. With the introduction of union types, we can now do better. |
| 78 | + A type like `String` would not carry the `null` value. To express that a value could |
| 79 | + be `null`, one would use the union type `String | Null`. For backwards compatibility |
| 80 | + and Java interoperability, selecting on a value that's possibly `null` would still be permitted |
| 81 | + but would have a declared effect that a `NullPointerException` can be thrown (see next section). |
| 82 | + |
| 83 | + - Effect Capabilities |
| 84 | + |
| 85 | + (Planned) Scala so far is an impure functional programming language in that side effects |
| 86 | + are not tracked. We want to put in the hooks to allow to change this over time. The idea |
| 87 | + is to treat effects as capabilities represented as implicit parameters. Some effect types |
| 88 | + will be defined by the language, others can be added by libraries. Initially, the language |
| 89 | + will likely only cover exceptions as effect capabilities, but it can be extended later |
| 90 | + to mutations and possibly other effects. To ensure backwards compatibility, all effect |
| 91 | + capabilities are initially available in `Predef`. Unimporting effect capabilities from |
| 92 | + `Predef` will enable stricter effect checking, and provide stronger guarantees of purity. |
| 93 | + |
| 94 | +<a name="ergonomics"></a> |
| 95 | +## Ergonomics |
| 96 | + |
| 97 | +Constructs that make common programming patterns more concise and readable. |
| 98 | + |
| 99 | + - Enums `enum Color { case Red, Green, Blue }` |
| 100 | + |
| 101 | + Enums give a simple way to express a type with a finite set of named values. They |
| 102 | + are found in most languages. The previous encodings of enums in Scala were all had |
| 103 | + problems that prevented universal adoption. The new native `enum` construct in Scala |
| 104 | + is quite flexile; among others it gives a more concise way to write algebraic data types, |
| 105 | + which would otherwise be expressed by a sealed base trait with case classes as alternatives. |
| 106 | + Scala enums will interop with en the host platform. They support multiversal equality |
| 107 | + out of the box, i.e. an enum can only be compared to values of the same enum type. |
| 108 | + |
| 109 | + - Extension clauses `extension StringOps for String { ... }` |
| 110 | + |
| 111 | + (Pending) Extension clauses allow to define extension methods and late implementations |
| 112 | + of traits via instance declarations. They are more readable and convey intent better |
| 113 | + than the previous encodings of these features through implicit classes and value classes. |
| 114 | + Extensions will replace implicit classes. Extensions and opaque types together can |
| 115 | + replace almost all usages of value classes. |
| 116 | + |
| 117 | +<a name="performance"></a> |
| 118 | +## Performance |
| 119 | + |
| 120 | + - Opaque Type Aliases `opaque type A = T` |
| 121 | + |
| 122 | + (Pending) An opaque alias allows to define a new type `A` in terms of an existing type `T`. Unlike the previous modeling using value classes, opqaue types guarantee no boxing. |
| 123 | + Opaque types are described in detail in [SIP 35](https://docs.scala-lang.org/sips/opaque-types.html). |
| 124 | + |
| 125 | + - Erased parameters |
| 126 | + |
| 127 | + Parameters of methods and functions can be declared `erased`. This means that |
| 128 | + the corresponding arguments are only used for type checking purposes and no code |
| 129 | + will be generated for them. Typical candidates for erased parameters are type |
| 130 | + constraints impressed through implicits such as `=:=` and `<:<`. Erased parameters |
| 131 | + help both runtime (since no argument has to be constructed) and compile time |
| 132 | + (since potentially large arguments can be eliminated early). |
| 133 | + |
| 134 | + |
0 commit comments