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

doc: document range match branch syntax (#5913)

This commit is contained in:
spaceface777 2020-07-22 02:31:32 +02:00 committed by GitHub
parent a370dd2867
commit 6d09842852
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -813,6 +813,24 @@ A match statement can also be used to branch on the variants of an `enum`
by using the shorthand `.variant_here` syntax. An `else` branch is not allowed
when all the branches are exhaustive.
```v
c := `v`
typ := match c {
`0`...`9` { 'digit' }
`A`...`Z` { 'uppercase' }
`a`...`z` { 'lowercase' }
else { 'other' }
}
println(typ) // 'lowercase'
```
You can also use ranges as `match` patterns. If the value falls within the range
of a branch, that branch will be executed.
Note that the ranges use `...` (three dots) rather than `..` (two dots). This is
because the range is *inclusive* of the last element, rather than exclusive
(as `..` ranges are). Using `..` in a match branch will throw an error.
### Defer
A defer statement defers the execution of a block of statements until the surrounding function returns.