mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
doc: move match next to if (#16304)
This commit is contained in:
182
doc/docs.md
182
doc/docs.md
@ -1565,6 +1565,97 @@ match mut x {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Match
|
||||||
|
|
||||||
|
```v
|
||||||
|
os := 'windows'
|
||||||
|
print('V is running on ')
|
||||||
|
match os {
|
||||||
|
'darwin' { println('macOS.') }
|
||||||
|
'linux' { println('Linux.') }
|
||||||
|
else { println(os) }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
A match statement is a shorter way to write a sequence of `if - else` statements.
|
||||||
|
When a matching branch is found, the following statement block will be run.
|
||||||
|
The else branch will be run when no other branches match.
|
||||||
|
|
||||||
|
```v
|
||||||
|
number := 2
|
||||||
|
s := match number {
|
||||||
|
1 { 'one' }
|
||||||
|
2 { 'two' }
|
||||||
|
else { 'many' }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
A match statement can also to be used as an `if - else if - else` alternative:
|
||||||
|
|
||||||
|
```v
|
||||||
|
match true {
|
||||||
|
2 > 4 { println('if') }
|
||||||
|
3 == 4 { println('else if') }
|
||||||
|
2 == 2 { println('else if2') }
|
||||||
|
else { println('else') }
|
||||||
|
}
|
||||||
|
// 'else if2' should be printed
|
||||||
|
```
|
||||||
|
|
||||||
|
or as an `unless` alternative: [unless Ruby](https://www.tutorialspoint.com/ruby/ruby_if_else.htm)
|
||||||
|
|
||||||
|
```v
|
||||||
|
match false {
|
||||||
|
2 > 4 { println('if') }
|
||||||
|
3 == 4 { println('else if') }
|
||||||
|
2 == 2 { println('else if2') }
|
||||||
|
else { println('else') }
|
||||||
|
}
|
||||||
|
// 'if' should be printed
|
||||||
|
```
|
||||||
|
|
||||||
|
A match expression returns the value of the final expression from the matching branch.
|
||||||
|
|
||||||
|
```v
|
||||||
|
enum Color {
|
||||||
|
red
|
||||||
|
blue
|
||||||
|
green
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_red_or_blue(c Color) bool {
|
||||||
|
return match c {
|
||||||
|
.red, .blue { true } // comma can be used to test multiple values
|
||||||
|
.green { false }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Note: `match` as an expression is not usable in `for` loop and `if` statements.
|
||||||
|
|
||||||
### In operator
|
### In operator
|
||||||
|
|
||||||
`in` allows to check whether an array or a map contains an element.
|
`in` allows to check whether an array or a map contains an element.
|
||||||
@ -1807,97 +1898,6 @@ The above code prints:
|
|||||||
7
|
7
|
||||||
```
|
```
|
||||||
|
|
||||||
### Match
|
|
||||||
|
|
||||||
```v
|
|
||||||
os := 'windows'
|
|
||||||
print('V is running on ')
|
|
||||||
match os {
|
|
||||||
'darwin' { println('macOS.') }
|
|
||||||
'linux' { println('Linux.') }
|
|
||||||
else { println(os) }
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
A match statement is a shorter way to write a sequence of `if - else` statements.
|
|
||||||
When a matching branch is found, the following statement block will be run.
|
|
||||||
The else branch will be run when no other branches match.
|
|
||||||
|
|
||||||
```v
|
|
||||||
number := 2
|
|
||||||
s := match number {
|
|
||||||
1 { 'one' }
|
|
||||||
2 { 'two' }
|
|
||||||
else { 'many' }
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
A match statement can also to be used as an `if - else if - else` alternative:
|
|
||||||
|
|
||||||
```v
|
|
||||||
match true {
|
|
||||||
2 > 4 { println('if') }
|
|
||||||
3 == 4 { println('else if') }
|
|
||||||
2 == 2 { println('else if2') }
|
|
||||||
else { println('else') }
|
|
||||||
}
|
|
||||||
// 'else if2' should be printed
|
|
||||||
```
|
|
||||||
|
|
||||||
or as an `unless` alternative: [unless Ruby](https://www.tutorialspoint.com/ruby/ruby_if_else.htm)
|
|
||||||
|
|
||||||
```v
|
|
||||||
match false {
|
|
||||||
2 > 4 { println('if') }
|
|
||||||
3 == 4 { println('else if') }
|
|
||||||
2 == 2 { println('else if2') }
|
|
||||||
else { println('else') }
|
|
||||||
}
|
|
||||||
// 'if' should be printed
|
|
||||||
```
|
|
||||||
|
|
||||||
A match expression returns the value of the final expression from the matching branch.
|
|
||||||
|
|
||||||
```v
|
|
||||||
enum Color {
|
|
||||||
red
|
|
||||||
blue
|
|
||||||
green
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_red_or_blue(c Color) bool {
|
|
||||||
return match c {
|
|
||||||
.red, .blue { true } // comma can be used to test multiple values
|
|
||||||
.green { false }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
Note: `match` as an expression is not usable in `for` loop and `if` statements.
|
|
||||||
|
|
||||||
### Defer
|
### Defer
|
||||||
|
|
||||||
A defer statement defers the execution of a block of statements
|
A defer statement defers the execution of a block of statements
|
||||||
|
Reference in New Issue
Block a user