-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes-showcase.fss
More file actions
104 lines (77 loc) · 2.92 KB
/
types-showcase.fss
File metadata and controls
104 lines (77 loc) · 2.92 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
type Address =
{ City: string
Zip: int }
type User =
{ Id: int
Name: string
Tags: string list
Address: Address option
Scores: int map
Pair: (int * string) }
type Subscription =
| Free
| Premium of int
let scores =
{ ["math"] = 20; ["science"] = 18 }
let statusByCode =
{ ["200"] = "ok"; ["404"] = "not-found" }
let user = { Id = 1; Name = "Ada"; Tags = ["engineer"; "writer"]; Address = Some { City = "London"; Zip = 12345 }; Scores = scores; Pair = (7, "seven") }
let subscription = Premium 12
// Helper typed with OfficeAddress.
let make_office_address (address: Address) = address
let officeAddress = make_office_address { City = "London"; Zip = 12345 }
// Same shape as Address, inferred structurally from literal.
let parisAddress = { City = "Paris"; Zip = 75000 }
let updated = { user with Tags = user.Tags @ ["mathematician"] }
// Inline structural annotation: any record with matching fields is accepted.
let format_address (address: { City: string; Zip: int }) = $"{address.City} ({address.Zip})"
// Name exposure for host capability maps.
let capabilities =
// Compact multiline map layout.
{ ["format_address"] = "address formatting"
["make_office_address"] = "office address constructor" }
let city =
match updated.Address with
| Some address -> address.City
| None -> "unknown"
let mathScore =
updated.Scores
|> Map.tryGet "math"
|> Option.defaultValue 0
let hasScience = updated.Scores |> Map.containsKey "science"
let formatAddressKey = "format_address"
let scienceKey = "science"
let okCode = "200"
let patchedScores = { ["science"] = 19; ..updated.Scores }
let mapPreview =
match updated.Scores with
| {} ->
"empty"
| { [subject] = score; ..remaining } ->
$"first={subject}:{score}, remaining={Map.count remaining}"
let monthlyPrice =
match subscription with
| Free -> 0
| Premium amount -> amount
let missing = "missing"
let unknown = "unknown"
print $"User: {updated.Name}"
print $"City: {city}"
print $"Math score: {mathScore}"
print $"Has 'science': {hasScience}"
print $"Patched science score: {patchedScores |> Map.tryGet scienceKey |> Option.defaultValue 0}"
print $"Map preview: {mapPreview}"
print $"Subscription price: {monthlyPrice}"
// Structural record equivalence showcase:
// - literal with matching fields
// - value created through another named record type (OfficeAddress)
print $"Structural address from literal: {format_address parisAddress}"
print $"Structural address from OfficeAddress: {format_address officeAddress}"
print $"Capability for format_address: {Map.tryGet formatAddressKey capabilities |> Option.defaultValue missing}"
print $"HTTP 200: {statusByCode[okCode] |> Option.defaultValue unknown}"
updated.Tags |> List.iter print
match updated with
| { Id = id; Pair = (left, right) } ->
print $"Record pattern: id={id}, pair=({left}, {right})"
| _ ->
print "Unexpected"