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

doc: document optional if val := opt { (#5838)

This commit is contained in:
Nick Treleaven 2020-07-15 20:57:41 +01:00 committed by GitHub
parent f5e6a83a05
commit 2492066041
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1454,7 +1454,7 @@ user := repo.find_user_by_id(7) or {
### Handling optionals ### Handling optionals
There are three ways of handling an optional. The first method is to There are four ways of handling an optional. The first method is to
propagate the error: propagate the error:
```v ```v
@ -1495,6 +1495,9 @@ Here, you can either call `panic()` or `exit()`, which will stop the execution o
or use a control flow statement (`return`, `break`, `continue`, etc) to break from the current block. or use a control flow statement (`return`, `break`, `continue`, etc) to break from the current block.
Note that `break` and `continue` can only be used inside a `for` loop. Note that `break` and `continue` can only be used inside a `for` loop.
V does not have a way to forcibly "unwrap" an optional (as other languages do, for instance Rust's `unwrap()`
or Swift's `!`). To do this, use `or { panic(err) }` instead.
--- ---
The third method is to provide a default value at the end of the `or` block. In case of an error, The third method is to provide a default value at the end of the `or` block. In case of an error,
that value would be assigned instead, so it must have the same type as the content of the `Option` being handled. that value would be assigned instead, so it must have the same type as the content of the `Option` being handled.
@ -1509,8 +1512,18 @@ a := do_something('foo') or { 'default' } // a will be 'foo'
b := do_something('bar') or { 'default' } // b will be 'default' b := do_something('bar') or { 'default' } // b will be 'default'
``` ```
V does not have a way to forcibly "unwrap" an optional (as other languages do, for instance Rust's `unwrap()` ---
or Swift's `!`). To do this, use `or { panic(err) }` instead. The fourth method is to use `if` unwrapping:
```v
if resp := http.get(url) {
println(resp.text) // resp is a http.Response, not an optional
} else {
println(err)
}
```
Above, `http.get` returns a `?http.Response`. `resp` is only in scope for the first
`if` branch. `err` is only in scope for the `else` branch.
## Generics ## Generics