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:
36
doc/docs.md
36
doc/docs.md
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user