This document is a focused reference for match patterns on maps.
Map patterns use brace + keyed clauses:
match values with
| { ["name"] = v } -> v
| _ -> ""Keys may be string, int, bool, or cases of a union with no payload-bearing cases. All keys in one map share one type.
Captured keys have the map's actual key type. A captured union key is a union value, rather than its case name as a string.
type Priority = | Low | High
let describe values =
match values with
| { [High] = message } -> message
| _ -> "no high-priority entry"
let message = { [High] = "now"; [Low] = "later" } |> describedescribe infers string map[Priority] -> string. Map patterns match the requested entries and permit additional entries.
| { ["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.