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

docs: update match as syntax (#8056)

This commit is contained in:
Daniel Däschle 2021-01-12 12:10:44 +01:00 committed by GitHub
parent d4237aa7af
commit 4ffe3d83b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1975,10 +1975,6 @@ fn land(w World) {
`match` must have a pattern for each variant or have an `else` branch. `match` must have a pattern for each variant or have an `else` branch.
There are two ways to access the cast variant inside a match branch:
- the shadowed match variable
- using `as` to specify a variable name
```v ignore ```v ignore
struct Moon {} struct Moon {}
struct Mars {} struct Mars {}
@ -1997,21 +1993,9 @@ fn pass_time(w World) {
Mars { w.shiver() } Mars { w.shiver() }
else {} else {}
} }
// using `as` to specify a name for each value
match w as var {
Mars { var.shiver() }
Venus { var.sweat() }
else {
// w is of type World
assert w is Moon
}
}
} }
``` ```
Note: shadowing only works when the match expression is a variable.
It will not work on struct fields, array indexes, or map keys.
### Option/Result types and error handling ### Option/Result types and error handling
Option types are declared with `?Type`: Option types are declared with `?Type`: