-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52a1e1f
commit 47a89e4
Showing
6 changed files
with
18 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
## Callbacks, closures and forward declarations `[language.proctypes]` | ||
|
||
Annotate `proc` type definitions and forward declarations with `{.raises [Defect], gcsafe.}` or specific exception types. | ||
Annotate `proc` type definitions and forward declarations with `{.raises [], gcsafe.}` or specific exception types. | ||
|
||
```nim | ||
# By default, Nim assumes closures may raise any exception and are not gcsafe | ||
# By annotating the callback with raises and gcsafe, the compiler ensures that | ||
# any functions assigned to the closure fit the given constraints | ||
type Callback = proc(...) {.raises: [Defect], gcsafe.} | ||
type Callback = proc(...) {.raises: [], gcsafe.} | ||
``` | ||
|
||
### Practical notes | ||
|
||
* Without annotations, `raises Exception` and no GC-safety is assumed by the compiler, infecting deduction in the whole call stack | ||
* Without annotations, `{.raises [Exception].}` and no GC-safety is assumed by the compiler, infecting deduction in the whole call stack | ||
* Annotations constrain the functions being assigned to the callback to follow its declaration, simplifying calling the callback safely | ||
* In particular, callbacks are difficult to reason about when they raise exceptions - what should the caller of the callback do? | ||
|