You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
val (x: String) :: _ = xs // error: pattern's type String is more specialized
17
17
// than the right-hand side expression's type Any
18
18
```
19
-
This code gives a compile-time warning in Scala 3.1 (and also in Scala 3.0 under the `-source future` setting) whereas it will fail at runtime with a `ClassCastException` in Scala 2. In Scala 3.1, a pattern binding is only allowed if the pattern is _irrefutable_, that is, if the right-hand side's type conforms to the pattern's type. For instance, the following is OK:
19
+
This code gives a compile-time warning in Scala 3.2 (and also earlier Scala 3.x under the `-source future` setting) whereas it will fail at runtime with a `ClassCastException` in Scala 2. In Scala 3.2, a pattern binding is only allowed if the pattern is _irrefutable_, that is, if the right-hand side's type conforms to the pattern's type. For instance, the following is OK:
20
20
```scala
21
21
valpair= (1, true)
22
22
val (x, y) = pair
@@ -25,7 +25,7 @@ Sometimes one wants to decompose data anyway, even though the pattern is refutab
25
25
```scala
26
26
valfirst:: rest = elems // error
27
27
```
28
-
This works in Scala 2. In fact it is a typical use case for Scala 2's rules. But in Scala 3.1 it will give a warning. One can avoid the warning by marking the right-hand side with an [`@unchecked`](https://scala-lang.org/api/3.x/scala/unchecked.html) annotation:
28
+
This works in Scala 2. In fact it is a typical use case for Scala 2's rules. But in Scala 3.2 it will give a warning. One can avoid the warning by marking the right-hand side with an [`@unchecked`](https://scala-lang.org/api/3.x/scala/unchecked.html) annotation:
The new syntax is supported in Scala 3.0. However, to enable smooth cross compilation between Scala 2 and Scala 3, the changed behavior and additional type checks are only enabled under the `-source future` setting. They will be enabled by default in version 3.1 of the language.
59
+
The new syntax is supported in Scala 3.0. However, to enable smooth cross compilation between Scala 2 and Scala 3, the changed behavior and additional type checks are only enabled under the `-source future` setting. They will be enabled by default in version 3.2 of the language.
The implementation of pattern matching in Scala 3 was greatly simplified compared to Scala 2. From a user perspective, this means that Scala 3 generated patterns are a *lot* easier to debug, as variables all show up in debug modes and positions are correctly preserved.
7
+
The implementation of pattern matching in Scala 3 was greatly simplified compared to Scala 2. From a user perspective, this means that Scala 3 generated patterns are a _lot_ easier to debug, as variables all show up in debug modes and positions are correctly preserved.
8
8
9
9
Scala 3 supports a superset of Scala 2 [extractors](https://www.scala-lang.org/files/archive/spec/2.13/08-pattern-matching.html#extractor-patterns).
10
10
11
11
## Extractors
12
12
13
13
Extractors are objects that expose a method `unapply` or `unapplySeq`:
14
14
15
-
```Scala
15
+
```scala
16
16
defunapply[A](x: T)(implicitx: B):U
17
17
defunapplySeq[A](x: T)(implicitx: B):U
18
18
```
@@ -25,7 +25,7 @@ called variadic extractors, which enables variadic patterns.
25
25
26
26
Fixed-arity extractors expose the following signature:
27
27
28
-
```Scala
28
+
```scala
29
29
defunapply[A](x: T)(implicitx: B):U
30
30
```
31
31
@@ -36,7 +36,7 @@ The type `U` conforms to one of the following matches:
36
36
37
37
Or `U` conforms to the type `R`:
38
38
39
-
```Scala
39
+
```scala
40
40
typeR= {
41
41
defisEmpty:Boolean
42
42
defget:S
@@ -62,7 +62,7 @@ A usage of a fixed-arity extractor is irrefutable if one of the following condit
62
62
63
63
Variadic extractors expose the following signature:
64
64
65
-
```Scala
65
+
```scala
66
66
defunapplySeq[A](x: T)(implicitx: B):U
67
67
```
68
68
@@ -73,7 +73,7 @@ The type `U` conforms to one of the following matches:
73
73
74
74
Or `U` conforms to the type `R`:
75
75
76
-
```Scala
76
+
```scala
77
77
typeR= {
78
78
defisEmpty:Boolean
79
79
defget:S
@@ -167,7 +167,7 @@ object Nat:
167
167
-`N > 1` is the maximum number of consecutive (parameterless `def` or `val`) `_1: P1 ... _N: PN` members in `U`
168
168
- Pattern-matching on exactly `N` patterns with types `P1, P2, ..., PN`
A bound like `: Ord` on a type parameter `T` of a method or class indicates a context parameter `using Ord[T]`. The context parameter(s) generated from context bounds come last in the definition of the containing method or class. For instance,
13
+
A bound like `: Ord` on a type parameter `T` of a method or class indicates a context parameter `using Ord[T]`. The context parameter(s) generated from context bounds
14
+
are added as follows:
15
+
16
+
- If the method parameters end in an implicit parameter list or using clause,
17
+
context parameters are added in front of that list.
18
+
- Otherwise they are added as a separate parameter clause at the end.
19
+
20
+
Example:
14
21
15
22
```scala
16
23
deff[T:C1:C2, U:C3](x: T)(usingy: U, z: V):R
@@ -19,7 +26,7 @@ def f[T: C1 : C2, U: C3](x: T)(using y: U, z: V): R
19
26
would expand to
20
27
21
28
```scala
22
-
deff[T, U](x: T)(usingy: U, z: V)(usingC1[T], C2[T], C3[U]):R
Copy file name to clipboardExpand all lines: docs/_docs/reference/contextual/derivation.md
+32-10Lines changed: 32 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,27 +19,38 @@ The `derives` clause generates the following given instances for the `Eq`, `Orde
19
19
companion object of `Tree`,
20
20
21
21
```scala
22
-
given [T:Eq] :Eq[Tree[T]] =Eq.derived
23
-
given [T:Ordering] :Ordering[Tree] =Ordering.derived
24
-
given [T:Show] :Show[Tree] =Show.derived
22
+
given [T:Eq] :Eq[Tree[T]] =Eq.derived
23
+
given [T:Ordering] :Ordering[Tree[T]] =Ordering.derived
24
+
given [T:Show] :Show[Tree[T]] =Show.derived
25
25
```
26
26
27
27
We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Show` instances are _derived instances_.
28
28
29
29
### Types supporting `derives` clauses
30
30
31
31
All data types can have a `derives` clause. This document focuses primarily on data types which also have a given instance
32
-
of the `Mirror` type class available. Instances of the `Mirror` type class are generated automatically by the compiler
33
-
for,
34
-
35
-
+ enums and enum cases
36
-
+ case classes and case objects
37
-
+ sealed classes or traits that have only case classes and case objects as children
32
+
of the `Mirror` type class available.
38
33
39
34
`Mirror` type class instances provide information at the type level about the components and labelling of the type.
40
35
They also provide minimal term level infrastructure to allow higher level libraries to provide comprehensive
41
36
derivation support.
42
37
38
+
Instances of the `Mirror` type class are generated automatically by the compiler
39
+
unconditionally for:
40
+
- enums and enum cases,
41
+
- case objects.
42
+
43
+
Instances for `Mirror` are also generated conditionally for:
44
+
- case classes where the constructor is visible at the callsite (always true if the companion is not a case object)
45
+
- sealed classes and sealed traits where:
46
+
- there exists at least one child case,
47
+
- each child case is reachable from the parent's definition,
48
+
- if the sealed trait/class has no companion, then each child case is reachable from the callsite through the prefix of the type being mirrored,
49
+
- and where the compiler can generate a `Mirror` type class instance for each child case.
50
+
51
+
52
+
The `Mirror` type class definition is as follows:
53
+
43
54
```scala
44
55
sealedtraitMirror:
45
56
@@ -119,10 +130,21 @@ new Mirror.Product:
119
130
newLeaf(...)
120
131
```
121
132
133
+
If a Mirror cannot be generated automatically for a given type, an error will appear explaining why it is neither a supported
134
+
sum type nor a product type. For example, if `A` is a trait that is not sealed,
135
+
136
+
```
137
+
No given instance of type deriving.Mirror.Of[A] was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Of[A]:
138
+
* trait A is not a generic product because it is not a case class
139
+
* trait A is not a generic sum because it is not a sealed trait
140
+
```
141
+
142
+
122
143
Note the following properties of `Mirror` types,
123
144
124
145
+ Properties are encoded using types rather than terms. This means that they have no runtime footprint unless used and
125
146
also that they are a compile time feature for use with Scala 3's metaprogramming facilities.
147
+
+ There is no restriction against the mirrored type being a local or inner class.
126
148
+ The kinds of `MirroredType` and `MirroredElemTypes` match the kind of the data type the mirror is an instance for.
127
149
This allows `Mirror`s to support ADTs of all kinds.
128
150
+ There is no distinct representation type for sums or products (ie. there is no `HList` or `Coproduct` type as in
Returning from nested anonymous functions has been deprecated.
8
+
Returning from nested anonymous functions has been deprecated, and will produce a warning from version `3.2`.
9
9
10
10
Nonlocal returns are implemented by throwing and catching `scala.runtime.NonLocalReturnException`-s. This is rarely what is intended by the programmer. It can be problematic because of the hidden performance cost of throwing and catching exceptions. Furthermore, it is a leaky implementation: a catch-all exception handler can intercept a `NonLocalReturnException`.
0 commit comments