Skip to content

Commit 66c36e2

Browse files
authored
Fix conflicts with language-reference-stable 2: Rebase Revenge (#16217)
2 parents 5b7a8dd + 5455c89 commit 66c36e2

29 files changed

+301
-286
lines changed

docs/_docs/reference/changed-features/compiler-plugins.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ title: "Changes in Compiler Plugins"
44
nightlyOf: https://docs.scala-lang.org/scala3/reference/changed-features/compiler-plugins.html
55
---
66

7-
Compiler plugins are supported by Dotty (and Scala 3) since 0.9. There are two notable changes
8-
compared to `scalac`:
7+
Compiler plugins are supported in Scala 3 since Dotty 0.9. There are two notable changes
8+
compared to Scala 2:
99

1010
- No support for analyzer plugins
1111
- Added support for research plugins
1212

13-
[Analyzer plugins][1] in `scalac` run during type checking and may influence
13+
[Analyzer plugins][1] run in Scala 2 during type checking and may influence
1414
normal type checking. This is a very powerful feature but for production usages,
1515
a predictable and consistent type checker is more important.
1616

1717
For experimentation and research, Scala 3 introduces _research plugin_. Research plugins
18-
are more powerful than `scalac` analyzer plugins as they let plugin authors customize
18+
are more powerful than Scala 2 analyzer plugins as they let plugin authors customize
1919
the whole compiler pipeline. One can easily replace the standard typer by a custom one or
2020
create a parser for a domain-specific language. However, research plugins are only
2121
enabled for nightly or snaphot releases of Scala 3.
@@ -26,7 +26,7 @@ _standard plugins_ in Scala 3. In terms of features, they are similar to
2626

2727
## Using Compiler Plugins
2828

29-
Both standard and research plugins can be used with `scalac` by adding the `-Xplugin:` option:
29+
In Scala 3, both standard and research plugins can be used with `scalac` by adding the `-Xplugin:` option:
3030

3131
```shell
3232
scalac -Xplugin:pluginA.jar -Xplugin:pluginB.jar Test.scala
@@ -40,7 +40,7 @@ the fully qualified plugin class name. The format of a property file is as follo
4040
pluginClass=dividezero.DivideZero
4141
```
4242

43-
This is different from `scalac` plugins that required a `scalac-plugin.xml` file.
43+
This is different from Scala 2 plugins that require a `scalac-plugin.xml` file.
4444

4545
Starting from 1.1.5, `sbt` also supports Scala 3 compiler plugins. Please refer to the
4646
[`sbt` documentation][2] for more information.

docs/_docs/reference/changed-features/eta-expansion-spec.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ implicit val bla: Double = 1.0
5151
val bar = foo // val bar: Int => Float = ...
5252
```
5353

54-
## Automatic Eta-Expansion and query types
54+
## Automatic Eta-Expansion and context types
5555

5656
A method with context parameters can be expanded to a value of a context type by writing the expected context type explicitly.
5757

@@ -66,7 +66,7 @@ val bar: Double ?=> Float = foo(3)
6666
- If `m` is has an empty argument list (i.e. has type `()R`):
6767
1. If the expected type is of the form `() => T`, we eta expand.
6868
2. If m is defined by Java, or overrides a Java defined method, we insert `()`.
69-
3. Otherwise we issue an error of the form:
69+
3. Otherwise we issue an error of the form: `method must be called with () argument`
7070

7171
Thus, an unapplied method with an empty argument list is only converted to a function when a function type is expected. It is considered best practice to either explicitly apply the method to `()`, or convert it to a function with `() => m()`.
7272

docs/_docs/reference/changed-features/main-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ final class happyBirthday:
7272
case error: CLP.ParseError => CLP.showError(error)
7373
```
7474

75-
**Note**: The `<static>` modifier above expresses that the `main` method is generated
75+
**Note:** The `<static>` modifier above expresses that the `main` method is generated
7676
as a static method of class `happyBirthday`. It is not available for user programs in Scala. Regular "static" members are generated in Scala using objects instead.
7777

7878
[`@main`](https://scala-lang.org/api/3.x/scala/main.html) methods are the recommended scheme to generate programs that can be invoked from the command line in Scala 3. They replace the previous scheme to write program as objects with a special `App` parent class. In Scala 2, `happyBirthday` could be written also like this:

docs/_docs/reference/changed-features/pattern-matching.md

Lines changed: 95 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,38 @@ Scala 3 supports a superset of Scala 2 [extractors](https://www.scala-lang.org/f
1313
Extractors are objects that expose a method `unapply` or `unapplySeq`:
1414

1515
```scala
16-
def unapply[A](x: T)(implicit x: B): U
17-
def unapplySeq[A](x: T)(implicit x: B): U
16+
def unapply(x: T): U
17+
def unapplySeq(x: T): U
18+
```
19+
20+
Where `T` is an arbitrary type, if it is a subtype of the scrutinee's type `Scrut`, a [type test](../other-new-features/type-test.md) is performed before calling the method.
21+
`U` follows rules described in [Fixed Arity Extractors](#fixed-arity-extractors) and [Variadic Extractors](#variadic-extractors).
22+
23+
**Note:** `U` can be the type of the extractor object.
24+
25+
`unapply` and `unapplySeq` can actually have a more general signature, allowing for a leading type clause, as well as arbitrarily many using clauses, both before and after the regular term clause, and at most one implicit clause at the end, for example:
26+
27+
```scala
28+
def unapply[A, B](using C)(using D)(x: T)(using E)(using F)(implicit y: G): U = ???
1829
```
1930

2031
Extractors that expose the method `unapply` are called fixed-arity extractors, which
2132
work with patterns of fixed arity. Extractors that expose the method `unapplySeq` are
2233
called variadic extractors, which enables variadic patterns.
2334

24-
### Fixed-Arity Extractors
35+
## Fixed-Arity Extractors
36+
37+
Fixed-arity extractors expose the following signature (with potential type, using and implicit clauses):
2538

26-
Fixed-arity extractors expose the following signature:
2739

2840
```scala
29-
def unapply[A](x: T)(implicit x: B): U
41+
def unapply(x: T): U
3042
```
3143

3244
The type `U` conforms to one of the following matches:
3345

34-
- Boolean match
35-
- Product match
46+
- [Boolean match](#boolean-match)
47+
- [Product match](#product-match)
3648

3749
Or `U` conforms to the type `R`:
3850

@@ -45,53 +57,24 @@ type R = {
4557

4658
and `S` conforms to one of the following matches:
4759

48-
- single match
49-
- name-based match
60+
- [single match](#single-match)
61+
- [name-based match](#name-based-match)
5062

5163
The former form of `unapply` has higher precedence, and _single match_ has higher
5264
precedence over _name-based match_.
5365

66+
**Note:** the `S` in `R` can be `U`.
67+
5468
A usage of a fixed-arity extractor is irrefutable if one of the following condition holds:
5569

5670
- `U = true`
5771
- the extractor is used as a product match
58-
- `U = Some[T]` (for Scala 2 compatibility)
5972
- `U <: R` and `U <: { def isEmpty: false }`
73+
- `U = Some[T]`
6074

61-
### Variadic Extractors
62-
63-
Variadic extractors expose the following signature:
64-
65-
```scala
66-
def unapplySeq[A](x: T)(implicit x: B): U
67-
```
68-
69-
The type `U` conforms to one of the following matches:
70-
71-
- sequence match
72-
- product-sequence match
73-
74-
Or `U` conforms to the type `R`:
75-
76-
```scala
77-
type R = {
78-
def isEmpty: Boolean
79-
def get: S
80-
}
81-
```
82-
83-
and `S` conforms to one of the two matches above.
84-
85-
The former form of `unapplySeq` has higher priority, and _sequence match_ has higher
86-
precedence over _product-sequence match_.
87-
88-
A usage of a variadic extractor is irrefutable if one of the following conditions holds:
89-
90-
- the extractor is used directly as a sequence match or product-sequence match
91-
- `U = Some[T]` (for Scala 2 compatibility)
92-
- `U <: R` and `U <: { def isEmpty: false }`
75+
**Note:** The last rule is necessary because, for compatibility reasons, `isEmpty` on `Some` has return type `Boolean` rather than `false`, even though it always returns `false`.
9376

94-
## Boolean Match
77+
### Boolean Match
9578

9679
- `U =:= Boolean`
9780
- Pattern-matching on exactly `0` patterns
@@ -111,10 +94,10 @@ object Even:
11194
// even has an even number of characters
11295
```
11396

114-
## Product Match
97+
### Product Match
11598

11699
- `U <: Product`
117-
- `N > 0` is the maximum number of consecutive (parameterless `def` or `val`) `_1: P1` ... `_N: PN` members in `U`
100+
- `N > 0` is the maximum number of consecutive (`val` or parameterless `def`) `_1: P1` ... `_N: PN` members in `U`
118101
- Pattern-matching on exactly `N` patterns with types `P1, P2, ..., PN`
119102

120103
For example:
@@ -141,9 +124,11 @@ object FirstChars:
141124
// First: H; Second: i
142125
```
143126

144-
## Single Match
127+
### Single Match
145128

146-
- If there is exactly `1` pattern, pattern-matching on `1` pattern with type `U`
129+
- Pattern-matching on `1` pattern with type `S`
130+
131+
For example, where `Nat <: R`, `S = Int`:
147132

148133
<!-- To be kept in sync with tests/new/patmat-spec.scala -->
149134

@@ -162,27 +147,72 @@ object Nat:
162147
// 5 is a natural number
163148
```
164149

165-
## Name-based Match
150+
### Name-based Match
166151

167-
- `N > 1` is the maximum number of consecutive (parameterless `def` or `val`) `_1: P1 ... _N: PN` members in `U`
152+
- `S` has `N > 1` members such that they are each `val`s or parameterless `def`s, and named from `_1` with type `P1` to `_N` with type `PN`
153+
- `S` doesn't have `N+1` members satisfying the previous point, i.e. `N` is maximal
168154
- Pattern-matching on exactly `N` patterns with types `P1, P2, ..., PN`
169155

156+
For example, where `U = AlwaysEmpty.type <: R`, `S = NameBased`:
170157
```scala
171-
object ProdEmpty:
158+
object MyPatternMatcher:
159+
def unapply(s: String) = AlwaysEmpty
160+
161+
object AlwaysEmpty:
162+
def isEmpty = true
163+
def get = NameBased
164+
165+
object NameBased:
172166
def _1: Int = ???
173167
def _2: String = ???
174-
def isEmpty = true
175-
def unapply(s: String): this.type = this
176-
def get = this
177168

178169
"" match
179-
case ProdEmpty(_, _) => ???
170+
case MyPatternMatcher(_, _) => ???
180171
case _ => ()
181172
```
182173

183-
## Sequence Match
174+
## Variadic Extractors
175+
176+
Variadic extractors expose the following signature (with potential type, using and implicit clauses):
177+
178+
```scala
179+
def unapplySeq(x: T): U
180+
```
181+
182+
Where `U` has to fullfill the following:
183+
184+
1. Set `V := U`
185+
2. `V` is valid if `V` conforms to one of the following matches:
186+
- [sequence match](#sequence-match)
187+
- [product-sequence match](#product-sequence-match)
188+
3. Otherwise `U` has to conform to the type `R`:
189+
```scala
190+
type R = {
191+
def isEmpty: Boolean
192+
def get: S
193+
}
194+
```
195+
4. Set `V := S`, and reattempt 2., if it fails `U` is not valid.
196+
197+
The `V := U` form of `unapplySeq` has higher priority, and _sequence match_ has higher
198+
precedence over _product-sequence match_.
199+
200+
**Note:** This means `isEmpty` is disregarded if the `V := U` form is valid
201+
202+
A usage of a variadic extractor is irrefutable if one of the following conditions holds:
203+
204+
- the extractor is used directly as a sequence match or product-sequence match
205+
- `U <: R` and `U <: { def isEmpty: false }`
206+
- `U = Some[T]`
207+
208+
**Note:** The last rule is necessary because, for compatibility reasons, `isEmpty` on `Some` has return type `Boolean` rather than `false`, even though it always returns `false`.
209+
210+
**Note:** Be careful, by the first condition and the note above, it is possible to define an irrefutable extractor with a `def isEmpty: true`.
211+
This is strongly discouraged and, if found in the wild, is almost certainly a bug.
212+
213+
### Sequence Match
184214

185-
- `U <: X`, `T2` and `T3` conform to `T1`
215+
- `V <: X`
186216

187217
```scala
188218
type X = {
@@ -192,10 +222,12 @@ type X = {
192222
def toSeq: scala.Seq[T3]
193223
}
194224
```
195-
225+
- `T2` and `T3` conform to `T1`
196226
- Pattern-matching on _exactly_ `N` simple patterns with types `T1, T1, ..., T1`, where `N` is the runtime size of the sequence, or
197227
- Pattern-matching on `>= N` simple patterns and _a vararg pattern_ (e.g., `xs: _*`) with types `T1, T1, ..., T1, Seq[T1]`, where `N` is the minimum size of the sequence.
198228

229+
For example, where `V = S`, `U = Option[S] <: R`, `S = Seq[Char]`
230+
199231
<!-- To be kept in sync with tests/new/patmat-spec.scala -->
200232

201233
```scala
@@ -211,14 +243,16 @@ object CharList:
211243
// e,x,a,m
212244
```
213245

214-
## Product-Sequence Match
246+
### Product-Sequence Match
215247

216-
- `U <: Product`
217-
- `N > 0` is the maximum number of consecutive (parameterless `def` or `val`) `_1: P1` ... `_N: PN` members in `U`
248+
- `V <: Product`
249+
- `N > 0` is the maximum number of consecutive (`val` or parameterless `def`) `_1: P1` ... `_N: PN` members in `V`
218250
- `PN` conforms to the signature `X` defined in Seq Pattern
219251
- Pattern-matching on exactly `>= N` patterns, the first `N - 1` patterns have types `P1, P2, ... P(N-1)`,
220252
the type of the remaining patterns are determined as in Seq Pattern.
221253

254+
For example, where `V = S`, `U = Option[S] <: R`, `S = (String, PN) <: Product`, `PN = Seq[Int]`
255+
222256
```scala
223257
class Foo(val name: String, val children: Int*)
224258
object Foo:
@@ -227,7 +261,7 @@ object Foo:
227261

228262
def foo(f: Foo) = f match
229263
case Foo(name, x, y, ns*) => ">= two children."
230-
case Foo(name, ns*) => => "< two children."
264+
case Foo(name, ns*) => "< two children."
231265
```
232266

233267
There are plans for further simplification, in particular to factor out _product match_

0 commit comments

Comments
 (0)