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

docs: add concise control flow example for if x := expr { (#17983)

This commit is contained in:
Turiiya 2023-04-25 06:07:28 +02:00 committed by GitHub
parent 3622544695
commit b87ddf68ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1538,6 +1538,49 @@ println(s)
// "odd"
```
Anywhere you can use `or {}`, you can also use "if unwrapping". This binds the unwrapped value
of an expression to a variable when that expression is not none nor an error.
```v
m := {
'foo': 'bar'
}
// handle missing keys
if v := m['foo'] {
println(v) // bar
} else {
println('not found')
}
```
```v
fn res() !int {
return 42
}
// functions that return a result type
if v := res() {
println(v)
}
```
```v
struct User {
name string
}
arr := [User{'John'}]
// if unwrapping with assignment of a variable
u_name := if v := arr[0] {
v.name
} else {
'Unnamed'
}
println(u_name) // John
```
#### Type checks and casts
You can check the current type of a sum type using `is` and its negated form `!is`.