docs: add a small sumtype match example in the Match section too

This commit is contained in:
Delyan Angelov 2024-11-25 21:11:05 +02:00
parent 19318110d9
commit 95e5ba44ae
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED

View file

@ -1947,6 +1947,23 @@ println(typ)
// '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
of a branch, that branch will be executed.