-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrecord_patterns.ail
More file actions
133 lines (113 loc) · 3.48 KB
/
Copy pathrecord_patterns.ail
File metadata and controls
133 lines (113 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
-- record_patterns.ail - Record Pattern Matching Examples
-- Demonstrates record destructuring in pattern matching (v0.6.2+)
-- =======================
-- BASIC SHORTHAND PATTERNS
-- =======================
-- {name, ...} binds field "name" and accepts extra fields via open-record syntax.
-- Pre-v0.22.0 a closed-record pattern {name} accepted records with extra fields
-- by accident (over-polymorphic schemes hid the field-count mismatch). Now use
-- {name, ...} explicitly to accept records that carry additional fields.
let person = {name: "Alice", age: 30} in
match person {
{name, ...} => name
}
-- Output: "Alice"
-- Multiple field shorthand
let user = {name: "Bob", age: 25, active: true} in
match user {
{name, age} => "${name} is ${show(age)}"
}
-- Output: "Bob is 25"
-- =======================
-- RENAMING PATTERNS
-- =======================
-- {name: n} binds field "name" to variable "n"
let person = {name: "Charlie", age: 35} in
match person {
{name: n, age: a} => "${n} (${show(a)})"
}
-- Output: "Charlie (35)"
-- Mix shorthand and renaming
let config = {host: "localhost", port: 8080} in
match config {
{host, port: p} => "${host}:${show(p)}"
}
-- Output: "localhost:8080"
-- =======================
-- NESTED RECORD PATTERNS
-- =======================
-- Match nested records (open-record syntax accepts extra fields like 'email')
let data = {
user: {name: "Diana", email: "[email protected]"}
} in
match data {
{user: {name, ...}, ...} => name
}
-- Output: "Diana"
-- Deeper nesting with renaming
let company = {
ceo: {
profile: {name: "Eve", title: "CEO"}
}
} in
match company {
{ceo: {profile: {name: n, title: t}}} => "${n} - ${t}"
}
-- Output: "Eve - CEO"
-- =======================
-- PATTERN WITH REST
-- =======================
-- {name, ...} matches name field, ignores rest
let big_record = {name: "Frank", age: 40, city: "NYC", active: true} in
match big_record {
{name, ...} => name
}
-- Output: "Frank"
-- =======================
-- FUNCTION RETURNING EXTRACTED FIELD
-- =======================
-- Extract field using record pattern in match. The lambda parameter takes
-- the SHAPE inferred from the pattern, so the call site must pass a record
-- with exactly that shape. Open-record patterns inside lambdas don't yet
-- propagate row polymorphism through the lambda parameter — pass matching
-- shapes when using lambda + match together, or use top-level `match` for
-- open-record patterns over records with extra fields.
let getName = \obj.
match obj {
{name} => name
}
in
getName({name: "Grace"})
-- Output: "Grace"
-- Extract nested field (matching shape exactly)
let getEmail = \obj.
match obj {
{user: {email: e}} => e
}
in
getEmail({user: {email: "[email protected]"}})
-- Output: "[email protected]"
-- =======================
-- MULTIPLE CASES WITH RECORDS
-- =======================
-- Multiple match arms (use COMMA, not pipe!)
type Status = Active | Inactive
let getPersonStatus = \person.
match person {
{name, status: Active} => "${name} is active",
{name, status: Inactive} => "${name} is inactive"
}
in
getPersonStatus({name: "Henry", status: Active})
-- Output: "Henry is active"
-- =======================
-- RECORDS WITH ADT FIELDS
-- =======================
-- Record containing Option value
type Option[a] = Some(a) | None
let maybeUser = {id: 1, email: Some("[email protected]")} in
match maybeUser {
{id, email: Some(e)} => "User ${show(id)}: ${e}",
{id, email: None} => "User ${show(id)}: no email"
}
-- Output: "User 1: [email protected]"