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/experimental/canthrow.md
+14Lines changed: 14 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -179,6 +179,20 @@ closure may refer to capabilities in its free variables. This means that `map` i
179
179
already effect polymorphic even though we did not change its signature at all.
180
180
So the takeaway is that the effects as capabilities model naturally provides for effect polymorphism whereas this is something that other approaches struggle with.
181
181
182
+
**Note 1:** The compiler will only treat checked exceptions that way. An exception type is _checked_ if it is a subtype of
183
+
`Exception` but not of `RuntimeException`. The signature of `CanThrow` still admits `RuntimeException`s since `RuntimeException` is a proper subtype of its bound, `Exception`. But no capabilities will be generated for `RuntimeException`s. Furthermore, `throws` clauses
184
+
also may not refer to `RuntimeException`s.
185
+
186
+
**Note 2:** To keep things simple, the compiler will currently only generate capabilities
187
+
for catch clauses of the form
188
+
```scala
189
+
caseex: Ex=>
190
+
```
191
+
where `ex` is an arbitrary variable name (`_` is also allowed), and `Ex` is an arbitrary
192
+
checked exception type. Constructor patterns such as `Ex(...)` or patterns with guards
193
+
are not allowed. The compiler will issue an error if one of these is used to catch
194
+
a checked exception and `saferExceptions` is enabled.
195
+
182
196
## Gradual Typing Via Imports
183
197
184
198
Another advantage is that the model allows a gradual migration from current unchecked exceptions to safer exceptions. Imagine for a moment that `experimental.saferExceptions` is turned on everywhere. There would be lots of code that breaks since functions have not yet been properly annotated with `throws`. But it's easy to create an escape hatch that lets us ignore the breakages for a while: simply add the import
0 commit comments