1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

all: add $res compile time function to get returned value in defer block (#18382)

This commit is contained in:
Louis Schmieder
2023-06-17 13:08:50 +02:00
committed by GitHub
parent ac32d2a803
commit dbd251793e
19 changed files with 223 additions and 10 deletions

View File

@@ -2119,6 +2119,42 @@ fn main() {
}
```
To access the result of the function inside a `defer` block the `$res()` expression can be used.
`$res()` is only used when a single value is returned, while on multi-return the `$res(idx)`
is parameterized.
```v ignore
fn (mut app App) auth_middleware() bool {
defer {
if !$res() {
app.response.status_code = 401
app.response.body = 'Unauthorized'
}
}
header := app.get_header('Authorization')
if header == '' {
return false
}
return true
}
fn (mut app App) auth_with_user_middleware() (bool, string) {
defer {
if !$res(0) {
app.response.status_code = 401
app.response.body = 'Unauthorized'
} else {
app.user = $res(1)
}
}
header := app.get_header('Authorization')
if header == '' {
return false, ''
}
return true, 'TestUser'
}
```
### Goto
V allows unconditionally jumping to a label with `goto`. The label name must be contained