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
Copy file name to clipboardExpand all lines: docs/docs/reference/changed-features/pattern-bindings.md
+4-6Lines changed: 4 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,9 +25,9 @@ want to decompose it like this:
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 type error. One can avoid the error by marking the pattern with an `@unchecked` annotation:
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 type error. One can avoid the error by marking the rhs with an `@unchecked` annotation:
29
29
```scala
30
-
valfirst::rest : @unchecked= elems// OK
30
+
valfirst:: rest =elems: @unchecked // OK
31
31
```
32
32
This will make the compiler accept the pattern binding. It might give an error at runtime instead, if the underlying assumption that `elems` can never be empty is wrong.
33
33
@@ -49,13 +49,11 @@ The filtering functionality can be obtained in Scala 3 by prefixing the pattern
49
49
50
50
## Syntax Changes
51
51
52
-
There are two syntax changes relative to Scala 2: First, pattern definitions can carry ascriptions such as `: @unchecked`. Second, generators in for expressions may be prefixed with `case`.
52
+
Generators in for expressions may be prefixed with `case`.
53
53
```
54
-
PatDef ::= ids [‘:’ Type] ‘=’ Expr
55
-
| Pattern2 [‘:’ Type | Ascription] ‘=’ Expr
56
54
Generator ::= [‘case’] Pattern1 ‘<-’ Expr
57
55
```
58
56
59
57
## Migration
60
58
61
-
The new syntax is supported in Dotty and 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 3.1` 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 3.1` setting. They will be enabled by default in version 3.1 of the language.
0 commit comments