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:
parent
a370dd2867
commit
6d09842852
18
doc/docs.md
18
doc/docs.md
@ -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
|
by using the shorthand `.variant_here` syntax. An `else` branch is not allowed
|
||||||
when all the branches are exhaustive.
|
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
|
### Defer
|
||||||
|
|
||||||
A defer statement defers the execution of a block of statements until the surrounding function returns.
|
A defer statement defers the execution of a block of statements until the surrounding function returns.
|
||||||
|
Loading…
Reference in New Issue
Block a user