Skip to content

Latest commit

 

History

History
85 lines (66 loc) · 1.59 KB

File metadata and controls

85 lines (66 loc) · 1.59 KB

FScript Map Matching Reference

Purpose

This document is a focused reference for match patterns on maps.

Map pattern basics

Map patterns use brace + keyed clauses:

match values with
| { ["name"] = v } -> v
| _ -> ""

Keys must be string.

Supported forms

Single key

| { ["a"] = x } -> ...

Multiple keys

| { ["a"] = x; ["b"] = y } -> ...

Tail capture

| { ["a"] = x; ..rest } -> ...

Dynamic key extraction

| { [k] = v; ..rest } -> ...

Guarded key checks

Use when for key equality checks (instead of pinning syntax):

| { [current] = v; ..rest } when current = key -> ...

Partial matching semantics

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.

Practical examples

Remove one key

let remove key values =
  match values with
  | { [current] = _; ..rest } when current = key -> rest
  | _ -> values

Match two required keys

let describe values =
  match values with
  | { ["name"] = name; ["owner"] = owner } -> $"{name} by {owner}"
  | _ -> "unknown"

Notes