From 2a86f11fb2d886a5a861dc42bc3dd53ce032d117 Mon Sep 17 00:00:00 2001 From: Alexander Satretdinov Date: Mon, 16 Nov 2026 16:29:29 +0200 Subject: [PATCH] docs: range patterns with |, arrays, guards; README match line --- README.md | 2 +- docs/src/language/pattern-matching.md | 43 +++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3256639..b0ddf26 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ skip: int() cannot parse "x" as an integer. - immutable by default (`let` / `let mut`), compile-time `const` - value semantics: arrays, objects, maps, sets and structs copy lazily, `mut` parameters are inout - structs and enums with fields, `impl` methods, `fun f(x) => expr;` -- `match` with exhaustiveness checks over enums; destructuring, spread, rest +- `match` on literals, ranges (`200..300`, `500..`), types, structs, arrays and enums, exhaustive over enum variants; destructuring, spread, rest - membership `x in xs` (a range without making it), slices `xs[1..]`, `s[..-1]` - nil-safe chains `a?.b ?? c`; `return` / `break` / `continue` / `throw` as expressions - `try`/`catch` by error type, as a statement or an expression; `defer` diff --git a/docs/src/language/pattern-matching.md b/docs/src/language/pattern-matching.md index 9753bba..ebf2415 100644 --- a/docs/src/language/pattern-matching.md +++ b/docs/src/language/pattern-matching.md @@ -40,10 +40,10 @@ pattern matches. built-in one like `string` or `nil`, `_ is T | U` one of them, the same [test](values.md#type-tests) as `v is T` and `catch e is T`; - literals: numbers, strings, `true`, `false`, `nil`, compared as by `==`; -- `a..b` matches a number from `a` up to `b`, `a..=b` up to `b` included, - `a..` one from `a` on and `..b` or `..=b` one up to `b`; a bound is a - number or the name of a constant. A float within the bounds matches, - and anything but a number is no match; +- `a..b` matches a number from `a` up to `b`, `b` excluded, `a..=b` up to + `b` included, `a..` one from `a` on and `..b` or `..=b` one up to `b`; a + bound is a number or the name of a constant. A float within the bounds + matches, and anything but a number, `NaN` included, is no match; - `p | q` matches if either does; - `Color.White` matches that [enum](../data/enums.md) variant, and `Expr.Num { value }` a variant with fields, as a struct pattern; @@ -101,12 +101,45 @@ print(grade({ status: 503, ms: 1 })); // error print(grade({ status: 404, ms: 1 })); // other ``` -A range no number is in is an error: +Ranges combine with `|`, sit in array patterns and take a guard; to use +the number, bind it with a name and test it in the guard: + +```rune +const LOW = 10; +const HIGH = 20; + +fun where(v) { + return match v { + -5..=-1 | LOW..HIGH => "in", + [..0, 1..] => "sign change", + 4.. if v % 2 == 0 => "even, 4 or more", + n if n is number => "number {n}", + _ => "other", + }; +} + +print(where(15)); // in +print(where([-1, 1])); // sign change +print(where(8)); // even, 4 or more +print(where(7)); // number 7 +print(where("15")); // other +``` + +A range no number is in is an error, and so is a bound that is not a +number or a constant holding one: ```rune match 1 { 5..5 => 1, _ => 2 } // Error at '..': Range pattern matches no number. ``` +```rune +let v = 1; +match 1 { v..3 => 1, _ => 2 } // Error at '..': A bound of a range pattern must be a number or a constant holding one. +``` + +Ranges never make a match exhaustive: `..0 | 0..` still needs `_ =>` +for values that are not numbers. + Object patterns pick data apart by shape, which makes small interpreters and event handlers short: