From 6d0984285224ed8e1228cfeec0e2ca5d31013072 Mon Sep 17 00:00:00 2001 From: spaceface777 Date: Wed, 22 Jul 2020 02:31:32 +0200 Subject: [PATCH] doc: document range match branch syntax (#5913) --- doc/docs.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index b58d30105a..6fc68a2b09 100644 --- a/doc/docs.md +++ b/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 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 A defer statement defers the execution of a block of statements until the surrounding function returns.