Skip to content

Commit 87cb34a

Browse files
authored
Merge pull request #4330 from dotty-staging/docs-overview
Add overview section of language changes to docs
2 parents ac84047 + bffb064 commit 87cb34a

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

docs/docs/reference/overview.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 address four major concerns:
8+
9+
- [Consistency](consistency) - improve orthogonality and eliminate restrictions.
10+
- [Safety](safety) - enable precise domain modeling and safe refactoring.
11+
- [Ergonomics](ergonomics) - support readable and concise code.
12+
- [Performance](performance) - remove performance penalties for high-level code.
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. These are listed separately in the [Dropped Features](http://dotty.epfl.ch/docs) section.
16+
17+
Another important set of changes is about meta programming and generative programming. So far these have relied on a [macro system](https://docs.scala-lang.org/overviews/macros/overview.html) that had experimental status. This macro system will be replaced with a different solution that extends [principled meta programming](http://dotty.epfl.ch/docs/reference/principled-meta-programming.html) and [inline](http://dotty.epfl.ch/docs/reference/inline.html) definitions with some reflective capabilities. The current state of the full design and its ramifications for generative programming will be described elsewhere.
18+
19+
<a name="consistency"></a>
20+
## Consistency
21+
22+
The primary goal of the language constructs in this section is to make the language more consistent, both internally, and in relationship to its [foundations](http://www.scala-lang.org/blog/2016/02/03/essence-of-scala.html).
23+
24+
- [Intersection types](http://dotty.epfl.ch/docs/reference/intersection-types.html) `A & B`
25+
26+
They replace compounds 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 types](http://dotty.epfl.ch/docs/reference/implicit-function-types.html) `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 and performance
34+
as well. They can replace many uses of monads, offering better composability and an order of magnitude improvement in runtime speed.
35+
36+
- [Dependent function types](http://dotty.epfl.ch/docs/reference/dependent-function-types.html) `(x: T) => x.S`.
37+
38+
The result type of a method can refer to its parameters. We now extend the same capability
39+
to the result type of a function.
40+
41+
- [Trait parameters](http://dotty.epfl.ch/docs/reference/trait-parameters.html) `trait T(x: S)`
42+
43+
Traits can now have value parameters, just like classes do. This replaces the more complex [early initializer](http://dotty.epfl.ch/docs/reference/dropped/early-initializers.html) syntax.
44+
45+
- Generic tuples
46+
47+
([Pending](https://github.com/lampepfl/dotty/pull/2199)) Generic tuples treat a tuple with arbitrary elements as a nested sequence of pairs. E.g. `(a, b, c)` is shorthand `(a, (b, (c, ())))`. This lets us drop the current limit of 22 for maximal tuple length and allows generic programs over tuples analogous to what is done for `HList`s.
48+
49+
<a name="safety"></a>
50+
## Safety
51+
52+
Listed in this section are new language constructs that help precise, typechecked domain modeling and that improve the reliability of refactorings.
53+
54+
- [Union types](http://dotty.epfl.ch/docs/reference/union-types.html) `A | B`
55+
56+
Union types gives fine-grained control over the possible values of a type.
57+
A union type `A | B` states that a value can be an `A` or a `B` without having
58+
to widen to a common supertype of `A` and `B`. Union types thus enable more
59+
precise domain modeling. They are also very useful for interoperating with
60+
Javascript libraries and JSON protocols.
61+
62+
- [Multiversal Equality](http://dotty.epfl.ch/docs/reference/multiversal-equality.html)
63+
64+
Multiversal equality is an opt-in way to check that comparisons using `==` and
65+
`!=` only apply to compatible types. It thus removes the biggest remaining hurdle
66+
to type-based refactoring. Normally, one would wish that one could change the type
67+
of some value or operation in a large code base, fix all type errors, and obtain
68+
at the end a working program. But universal equality `==` works for all types.
69+
So what should conceptually be a type error would not be reported and
70+
runtime behavior would change instead. Multiversal equality closes that loophole.
71+
72+
- Null safety
73+
74+
(Planned) Adding a `null` value to every type has been called a "Billion Dollar Mistake"
75+
by its creator, Tony Hoare. With the introduction of union types, we can now do better.
76+
A type like `String` will not carry the `null` value. To express that a value can
77+
be `null`, one will use the union type `String | Null` instead. For backwards compatibility and Java interoperability, selecting on a value that's possibly `null` will still be permitted but will have a declared effect that a `NullPointerException` can be thrown (see next section).
78+
79+
- Effect Capabilities
80+
81+
(Planned) Scala so far is an impure functional programming language in that side effects
82+
are not tracked. We want to put in the hooks to allow to change this over time. The idea
83+
is to treat effects as capabilities represented as implicit parameters. Some effect types
84+
will be defined by the language, others can be added by libraries. Initially, the language
85+
will likely only cover exceptions as effect capabilities, but this can be extended later
86+
to mutations and other effects. To ensure backwards compatibility, all effect
87+
capabilities are initially available in `Predef`. Un-importing effect capabilities from
88+
`Predef` will enable stricter effect checking, and provide stronger guarantees of purity.
89+
90+
<a name="ergonomics"></a>
91+
## Ergonomics
92+
93+
The primary goal of the language constructs in this section is to make common programming patterns more concise and readable.
94+
95+
- [Enums](http://dotty.epfl.ch/docs/reference/enums/enums.html) `enum Color { case Red, Green, Blue }`
96+
97+
Enums give a simple way to express a type with a finite set of named values. They
98+
are found in most languages. The previous encodings of enums as library-defined types
99+
were not fully satisfactory and consequently were not adopted widely. The new native `enum` construct in Scala is quite flexible; among others it gives a more concise way to express [algebraic data types](http://dotty.epfl.ch/docs/reference/enums/adts.html).
100+
Scala enums will interoperate with the host platform. They support multiversal equality
101+
out of the box, i.e. an enum can only be compared to values of the same enum type.
102+
103+
- [Type lambdas](http://dotty.epfl.ch/docs/reference/type-lambdas.html) `[X] => C[X]`
104+
105+
Type lambdas were encoded previously in a roundabout way, exploiting
106+
loopholes in Scala's type system which made it Turing complete. With
107+
the removal of [unrestricted type projection](dropped/type-projection.html), the loopholes are eliminated, so the
108+
previous encodings are no longer expressible. Type lambdas in the language provide
109+
a safe and more ergonomic alternative.
110+
111+
- Extension clauses `extension StringOps for String { ... }`
112+
113+
([Pending](https://github.com/lampepfl/dotty/pull/4114)) Extension clauses allow to define extension methods and late implementations
114+
of traits via instance declarations. They are more readable and convey intent better
115+
than the previous encodings of these features through implicit classes and value classes.
116+
Extensions will replace implicit classes. Extensions and opaque types together can
117+
replace almost all usages of value classes. Value classes are kept around for the
118+
time being since there might be a new good use case for them in the future if the host platform supports "structs" or some other way to express multi-field value classes.
119+
120+
<a name="performance"></a>
121+
## Performance
122+
123+
The primary goal of the language constructs in this section is to enable high-level, safe code without having to pay a performance penalty.
124+
125+
- Opaque Type Aliases `opaque type A = T`
126+
127+
([Pending](https://github.com/lampepfl/dotty/pull/4028)) An opaque alias defines a new type `A` in terms of an existing type `T`. Unlike the previous modeling using value classes, opaque types never box. Opaque types are described in detail in [SIP 35](https://docs.scala-lang.org/sips/opaque-types.html).
128+
129+
- [Erased parameters](http://dotty.epfl.ch/docs/reference/erased-terms.html)
130+
131+
Parameters of methods and functions can be declared `erased`. This means that
132+
the corresponding arguments are only used for type checking purposes and no code
133+
will be generated for them. Typical candidates for erased parameters are type
134+
constraints such as `=:=` and `<:<` that are expressed through implicits.
135+
Erased parameters improve both run times (since no argument has to be constructed) and compile times (since potentially large arguments can be eliminated early).

docs/sidebar.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ sidebar:
1414
- title: Dottydoc
1515
url: docs/usage/dottydoc.html
1616
- title: Reference
17+
- title: Overview
18+
- url: docs/reference/overview.html
1719
subsection:
1820
- title: New Types
1921
subsection:
@@ -27,8 +29,6 @@ sidebar:
2729
url: docs/reference/implicit-function-types.html
2830
- title: Dependent Function Types
2931
url: docs/reference/dependent-function-types.html
30-
- title: Literal Singleton Types
31-
url: docs/reference/singleton-types.html
3232
- title: Enums
3333
subsection:
3434
- title: Enumerations

0 commit comments

Comments
 (0)