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
The runtimeChecked implementation was accepted in the SIP meeting of May 23, 2025. Since the the feature has been around for long,
and is quite uncontroversial, I think we can just merge it for 3.8. No preview phase should be needed.
The `runtimeChecked` method is an extension method, defined in `scala.Predef`. It can be called on any expression. An expression ending in `.runtimeChecked` is exempt from certain static checks in the compiler, for example pattern match exhaustivity. The idiom is intended to replace a `: @unchecked` type ascription in these cases.
8
-
9
-
## Example
10
-
11
-
A common use case for `runtimeChecked` is to assert that a pattern will always match, either for convenience, or because there is a known invariant that the types can not express.
12
-
13
-
E.g. looking up an expected entry in a dynamically loaded dictionary-like structure:
// case Some(Sat | Sun) => // weekend should not appear
35
-
caseNone=>
36
-
```
37
-
38
-
In both of these cases, without `runtimeChecked` there would either be an error (example 1), or a warning (example 2), because statically, the compiler knows that there could be other cases at runtime - so is right to caution the programmer.
39
-
40
-
```scala
41
-
// warning in example 2 when we don't add `.runtimeChecked`.
|It would fail on pattern case:Some(Sat), Some(Sun)
48
-
```
49
-
50
-
## Safety
51
-
52
-
The `runtimeChecked` method only turns off static checks that can be soundly performed at runtime. This means that patterns with unchecked type-tests will still generate warnings. For example:
|the typetestfor::[Int] cannot be checked at runtime
63
-
|because its typearguments can't be determined from List[Any]
64
-
valres0:Int=1
65
-
```
66
-
As the warning hints, the type `::[Int]` can not be tested at runtime on a value of type `List[Any]`, so using `runtimeChecked` still protects the user against assertions that can not be validated.
67
-
68
-
To fully avoid warnings, as with previous Scala versions, `@unchecked` should be put on the type argument:
69
-
```scala
70
-
scala> xs.runtimeChecked match {
71
-
|caseis: ::[Int@unchecked] => is.head
72
-
| }
73
-
valres1:Int=1
74
-
```
75
-
76
-
77
-
## Specification
78
-
79
-
We add a new annotation `scala.internal.RuntimeChecked` as a part of the standard Scala 3 library. A programmer is not expected to use this annotation directly.
80
-
81
-
```scala
82
-
packagescala.annotation.internal
83
-
84
-
finalclassRuntimeCheckedextendsAnnotation
85
-
```
86
-
87
-
Any term that is the scrutinee of a pattern match, and that has a type annotated with `RuntimeChecked`, is exempt from pattern match exhaustivity checking.
88
-
89
-
90
-
The user facing API is augmented with a new extension method `scala.Predef.runtimeChecked`, qualified for any value:
91
-
```scala
92
-
packagescala
93
-
94
-
importscala.annotation.internal.RuntimeChecked
95
-
96
-
objectPredef:
97
-
...
98
-
extension [T](x: T)
99
-
inlinedefruntimeChecked: x.type@RuntimeChecked=
100
-
x: @RuntimeChecked
101
-
```
102
-
103
-
The `runtimeChecked` method returns its argument, refining its type with the `RuntimeChecked` annotation.
104
-
105
-
## Motivation
106
-
107
-
As described in [Pattern Bindings](../changed-features/pattern-bindings.md), under `-source:future` it is an error for a pattern definition to be refutable. For instance, consider:
108
-
```scala
109
-
defxs:List[Any] =???
110
-
valy:: ys = xs
111
-
```
112
-
113
-
This compiled without warning in 3.0, became a warning in 3.2, and we would like to make it an error by default in a future 3.x version.
114
-
As an escape hatch in 3.2 we recommended to use a type ascription of `: @unchecked`:
|pattern's type ::[Any] is more specialized than the right
120
-
|hand side expression's type List[Any]
121
-
|
122
-
|If the narrowing is intentional, this can be communicated
123
-
|by adding `: @unchecked` after the expression,
124
-
|which may result in a MatchError at runtime.
125
-
```
126
-
127
-
However, `: @unchecked` is syntactically awkward, and is also a misnomer - in fact in this case the pattern _is_ fully checked, but the necessary checks occur at runtime. The `runtimeChecked` method is intended to replace `@unchecked` for this purpose.
128
-
129
-
The `@unchecked` annotation is still retained for silencing warnings on unsound type tests.
130
-
131
-
### Restoring Scala 2.13 semantics with runtimeChecked
132
-
133
-
In Scala 3, the `: @unchecked` type ascription has the effect of turning off all pattern-match warnings on the match scrutinee - this differs from 2.13 in which it strictly turns off only pattern exhaustivity checking. `runtimeChecked` restores the semantics of Scala 2.13.
7
+
This is now a standard Scala 3 feature. See [https://docs.scala-lang.org/scala3/reference/experimental/runtimeChecked.html] for an up-to-date doc page.
The `runtimeChecked` method is an extension method, defined in `scala.Predef`. It can be called on any expression. An expression ending in `.runtimeChecked` is exempt from certain static checks in the compiler, for example pattern match exhaustivity. The idiom is intended to replace a `: @unchecked` type ascription in these cases.
8
+
9
+
## Example
10
+
11
+
A common use case for `runtimeChecked` is to assert that a pattern will always match, either for convenience, or because there is a known invariant that the types can not express.
12
+
13
+
E.g. looking up an expected entry in a dynamically loaded dictionary-like structure:
// case Some(Sat | Sun) => // weekend should not appear
35
+
caseNone=>
36
+
```
37
+
38
+
In both of these cases, without `runtimeChecked` there would either be an error (example 1), or a warning (example 2), because statically, the compiler knows that there could be other cases at runtime - so is right to caution the programmer.
39
+
40
+
```scala
41
+
// warning in example 2 when we don't add `.runtimeChecked`.
|It would fail on pattern case:Some(Sat), Some(Sun)
48
+
```
49
+
50
+
## Safety
51
+
52
+
The `runtimeChecked` method only turns off static checks that can be soundly performed at runtime. This means that patterns with unchecked type-tests will still generate warnings. For example:
|the typetestfor::[Int] cannot be checked at runtime
63
+
|because its typearguments can't be determined from List[Any]
64
+
valres0:Int=1
65
+
```
66
+
As the warning hints, the type `::[Int]` can not be tested at runtime on a value of type `List[Any]`, so using `runtimeChecked` still protects the user against assertions that can not be validated.
67
+
68
+
To fully avoid warnings, as with previous Scala versions, `@unchecked` should be put on the type argument:
69
+
```scala
70
+
scala> xs.runtimeChecked match {
71
+
|caseis: ::[Int@unchecked] => is.head
72
+
| }
73
+
valres1:Int=1
74
+
```
75
+
76
+
77
+
## Specification
78
+
79
+
We add a new annotation `scala.internal.RuntimeChecked` as a part of the standard Scala 3 library. A programmer is not expected to use this annotation directly.
80
+
81
+
```scala
82
+
packagescala.annotation.internal
83
+
84
+
finalclassRuntimeCheckedextendsAnnotation
85
+
```
86
+
87
+
Any term that is the scrutinee of a pattern match, and that has a type annotated with `RuntimeChecked`, is exempt from pattern match exhaustivity checking.
88
+
89
+
90
+
The user facing API is augmented with a new extension method `scala.Predef.runtimeChecked`, qualified for any value:
91
+
```scala
92
+
packagescala
93
+
94
+
importscala.annotation.internal.RuntimeChecked
95
+
96
+
objectPredef:
97
+
...
98
+
extension [T](x: T)
99
+
inlinedefruntimeChecked: x.type@RuntimeChecked=
100
+
x: @RuntimeChecked
101
+
```
102
+
103
+
The `runtimeChecked` method returns its argument, refining its type with the `RuntimeChecked` annotation.
104
+
105
+
## Motivation
106
+
107
+
As described in [Pattern Bindings](../changed-features/pattern-bindings.md), under `-source:future` it is an error for a pattern definition to be refutable. For instance, consider:
108
+
```scala
109
+
defxs:List[Any] =???
110
+
valy:: ys = xs
111
+
```
112
+
113
+
This compiled without warning in 3.0, became a warning in 3.2, and we would like to make it an error by default in a future 3.x version.
114
+
As an escape hatch in 3.2 we recommended to use a type ascription of `: @unchecked`:
|pattern's type ::[Any] is more specialized than the right
120
+
|hand side expression's type List[Any]
121
+
|
122
+
|If the narrowing is intentional, this can be communicated
123
+
|by adding `: @unchecked` after the expression,
124
+
|which may result in a MatchError at runtime.
125
+
```
126
+
127
+
However, `: @unchecked` is syntactically awkward, and is also a misnomer - in fact in this case the pattern _is_ fully checked, but the necessary checks occur at runtime. The `runtimeChecked` method is intended to replace `@unchecked` for this purpose.
128
+
129
+
The `@unchecked` annotation is still retained for silencing warnings on unsound type tests.
130
+
131
+
### Restoring Scala 2.13 semantics with runtimeChecked
132
+
133
+
In Scala 3, the `: @unchecked` type ascription has the effect of turning off all pattern-match warnings on the match scrutinee - this differs from 2.13 in which it strictly turns off only pattern exhaustivity checking. `runtimeChecked` restores the semantics of Scala 2.13.
0 commit comments