diff --git a/doc/docs.md b/doc/docs.md index 8f8bb40c31..0b3d3110e9 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -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.