mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
docs: add a small sumtype match example in the Match section too
This commit is contained in:
parent
19318110d9
commit
95e5ba44ae
1 changed files with 17 additions and 0 deletions
17
doc/docs.md
17
doc/docs.md
|
@ -1947,6 +1947,23 @@ println(typ)
|
||||||
// 'lowercase'
|
// 'lowercase'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A match statement also can match the variant types of a `sumtype`. Note that
|
||||||
|
in that case, the match is exhaustive, since all variant types are mentioned
|
||||||
|
explicitly, so there is no need for an `else{}` branch.
|
||||||
|
|
||||||
|
```v nofmt
|
||||||
|
struct Dog {}
|
||||||
|
struct Cat {}
|
||||||
|
struct Veasel {}
|
||||||
|
type Animal = Dog | Cat | Veasel
|
||||||
|
a := Animal(Veasel{})
|
||||||
|
match a {
|
||||||
|
Dog { println('Bay') }
|
||||||
|
Cat { println('Meow') }
|
||||||
|
Veasel { println('Vrrrrr-eeee') } // see: https://www.youtube.com/watch?v=qTJEDyj2N0Q
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
You can also use ranges as `match` patterns. If the value falls within the range
|
You can also use ranges as `match` patterns. If the value falls within the range
|
||||||
of a branch, that branch will be executed.
|
of a branch, that branch will be executed.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue