-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I implemented the 'defer' syntax earlier, and it's been pretty handy, but I've quite often hit the use case of only running defer statements if the script *fails*. That's precisely what defer does. I document it a bit on the MkDocs page. Here's a demo though: defer: print(1) print(2) errdefer: print(3) print(4) defer: print(5) print(6) errdefer: print(7) print(8) print("Hello!") exit(0) // successful script run outputs Hello! 5 6 1 2 i.e. errdefers don't run. The failure case: defer: print(1) print(2) errdefer: print(3) print(4) defer: print(5) print(6) errdefer: print(7) print(8) print("Hello!") exit(1) // perceived as error! outputs Hello! 7 8 5 6 3 4 1 2
- Loading branch information
Showing
8 changed files
with
114 additions
and
14 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
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
title: Defer & Errdefer | ||
--- | ||
|
||
- `defer` and `errdefer` run in LIFO order, each kind being part of the same one queue. | ||
- If there are several defer statements, and one fails, further defer statements will still attempt to run. | ||
- Rad's error code will become an error if the main script succeeded but a defer statement failed. | ||
- errdefers will not get triggered if the main script succeeded but a `defer` or `errdefer` statement failed. | ||
|
||
## `defer` | ||
|
||
```rsl title="defer Example" | ||
defer: | ||
print(1) | ||
print(2) | ||
defer: | ||
print(3) | ||
print(4) | ||
print("Hello!") | ||
``` | ||
|
||
```title="defer Example Output" | ||
Hello! | ||
3 | ||
4 | ||
1 | ||
2 | ||
``` | ||
|
||
## `errdefer` | ||
|
||
```rsl title="errdefer Example 1" | ||
defer: | ||
print(1) | ||
print(2) | ||
errdefer: | ||
print(3) | ||
print(4) | ||
defer: | ||
print(5) | ||
print(6) | ||
errdefer: | ||
print(7) | ||
print(8) | ||
print("Hello!") | ||
exit(0) // successful script run | ||
``` | ||
|
||
```title="errdefer Example 1 Output" | ||
Hello! | ||
5 | ||
6 | ||
1 | ||
2 | ||
``` | ||
|
||
```rsl title="errdefer Example 2" | ||
defer: | ||
print(1) | ||
print(2) | ||
errdefer: | ||
print(3) | ||
print(4) | ||
defer: | ||
print(5) | ||
print(6) | ||
errdefer: | ||
print(7) | ||
print(8) | ||
print("Hello!") | ||
exit(1) // perceived as error! | ||
``` | ||
|
||
```title="errdefer Example 2 Output" | ||
Hello! | ||
7 | ||
8 | ||
5 | ||
6 | ||
3 | ||
4 | ||
1 | ||
2 | ||
``` |