From 68af870744758de5057e0b19fa10c7578683d852 Mon Sep 17 00:00:00 2001 From: Guset0x0 Date: Tue, 26 Nov 2024 16:45:13 +0800 Subject: [PATCH] add doc for range pattern --- moonbit-docs/docs/README.md | 31 +++++++++++++++++++ .../current/README.md | 31 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/moonbit-docs/docs/README.md b/moonbit-docs/docs/README.md index 3fee96db..273845dd 100644 --- a/moonbit-docs/docs/README.md +++ b/moonbit-docs/docs/README.md @@ -1274,6 +1274,37 @@ match expr { } ``` +### Range Pattern +For builtin integer types and `Char`, MoonBit allows matching whether the value falls in a specific range. +Range patterns have the form `a.. Int { + match x { + _.. -1 + Zero => 0 + 1..<_ => 1 + } +} + +fn classify_char(c : Char) -> String { + match c { + 'a'..='z' => "lowercase" + 'A'..='Z' => "uppercase" + '0'..='9' => "digit" + _ => "other" + } +} +``` + ### Map Pattern MoonBit allows convenient matching on map-like data structures. diff --git a/moonbit-docs/i18n/zh/docusaurus-plugin-content-docs/current/README.md b/moonbit-docs/i18n/zh/docusaurus-plugin-content-docs/current/README.md index a12fe36e..006f99e9 100644 --- a/moonbit-docs/i18n/zh/docusaurus-plugin-content-docs/current/README.md +++ b/moonbit-docs/i18n/zh/docusaurus-plugin-content-docs/current/README.md @@ -1234,6 +1234,37 @@ match expr { } ``` +### 范围模式匹配 +对于内建的整数类型和字符类型 `Char`,MoonBit 允许匹配一个值是否落在某个范围内。 +范围模式的语法是 `a.. Int { + match x { + _.. -1 + Zero => 0 + 1..<_ => 1 + } +} + +fn classify_char(c : Char) -> String { + match c { + 'a'..='z' => "lowercase" + 'A'..='Z' => "uppercase" + '0'..='9' => "digit" + _ => "other" + } +} +``` + ### 键值对模式匹配 MoonBit 允许模式匹配字典等具有键值对结构的数据结构。在一个字典模式里,`key : value` 语法可以用来匹配 `key` 存在的情况,`value` 会被用于匹配 `key` 在键值对中的值。`key? : value` 语法无论 `key` 是否存在都能匹配,`value` 会被用于匹配 `map[key]` 的值(一个 `Option` 类型的值):