This document is a focused reference for match patterns on maps.
Map patterns use brace + keyed clauses:
match values with
| { ["name"] = v } -> v
| _ -> ""Keys must be string.
| { ["a"] = x } -> ...| { ["a"] = x; ["b"] = y } -> ...| { ["a"] = x; ..rest } -> ...| { [k] = v; ..rest } -> ...Use when for key equality checks (instead of pinning syntax):
| { [current] = v; ..rest } when current = key -> ...Map matching is partial:
- a pattern only requires the listed keys to exist,
- additional keys in the matched value are allowed.
Example:
match values with
| { ["name"] = name } -> $"name={name}"
| _ -> "no match"This still matches if values has extra keys.
let remove key values =
match values with
| { [current] = _; ..rest } when current = key -> rest
| _ -> valueslet describe values =
match values with
| { ["name"] = name; ["owner"] = owner } -> $"{name} by {owner}"
| _ -> "unknown"- Map literals:
{}empty map{ ["k"] = v }{ ["k"] = v; ..tail }
- For map construction/update rules, see
supported-types.md. - For full syntax/layout rules, see
syntax-and-indentation.md.