Skip to content

Update timeline for Scala 3 pattern binding behavior changes #2325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions _scala3-reference/changed-features/pattern-bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ next-page: /scala3/reference/changed-features/pattern-matching
In Scala 2, pattern bindings in `val` definitions and `for` expressions are
loosely typed. Potentially failing matches are still accepted at compile-time,
but may influence the program's runtime behavior.
From Scala 3.1 on, type checking rules will be tightened so that warnings are reported at compile-time instead.
From Scala 3.2 on, type checking rules will be tightened so that warnings are reported at compile-time instead.

## Bindings in Pattern Definitions

Expand All @@ -18,7 +18,7 @@ val xs: List[Any] = List(1, 2, 3)
val (x: String) :: _ = xs // error: pattern's type String is more specialized
// than the right-hand side expression's type Any
```
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:
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:
```scala
val pair = (1, true)
val (x, y) = pair
Expand All @@ -28,7 +28,7 @@ want to decompose it like this:
```scala
val first :: rest = elems // error
```
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` annotation:
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` annotation:
```scala
val first :: rest = elems: @unchecked // OK
```
Expand All @@ -43,7 +43,7 @@ val elems: List[Any] = List((1, 2), "hello", (3, 4))
for (x, y) <- elems yield (y, x) // error: pattern's type (Any, Any) is more specialized
// than the right-hand side expression's type Any
```
This code gives a compile-time warning in Scala 3.1 whereas in Scala 2 the list `elems`
This code gives a compile-time warning in Scala 3.2 whereas in Scala 2 the list `elems`
is filtered to retain only the elements of tuple type that match the pattern `(x, y)`.
The filtering functionality can be obtained in Scala 3 by prefixing the pattern with `case`:
```scala
Expand All @@ -59,4 +59,4 @@ Generator ::= [‘case’] Pattern1 ‘<-’ Expr

## Migration

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.
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.