Skip to content

Commit bf314c8

Browse files
committed
Update docs and samples for int/string map keys
1 parent 72709fb commit bf314c8

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

docs/embedding-fscript-language.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Host-visible static types are represented by `Type`:
141141
- `TList of Type`
142142
- `TTuple of Type list`
143143
- `TRecord of Map<string, Type>`
144-
- `TStringMap of Type`
144+
- `TMap of Type * Type`
145145
- `TOption of Type`
146146
- `TFun of Type * Type`
147147
- `TNamed of string`
@@ -150,6 +150,7 @@ Host-visible static types are represented by `Type`:
150150
- `TVar of int`
151151

152152
Use `Types.typeToString` for display/diagnostics.
153+
The postfix script type syntax `'a map` corresponds to `TMap(TString, 'a)`.
153154

154155
## Runtime values
155156

@@ -163,7 +164,7 @@ Evaluation returns `Value`:
163164
- `VList of Value list`
164165
- `VTuple of Value list`
165166
- `VRecord of Map<string, Value>`
166-
- `VStringMap of Map<string, Value>`
167+
- `VMap of Map<MapKey, Value>`
167168
- `VOption of Value option`
168169
- `VUnionCase of string * string * Value option`
169170
- `VUnionCtor of string * string`

docs/language-choices.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ let x =
2121
- `'a map`
2222
- Map values use native literals:
2323
- `{ ["a"] = 1; ["b"] = 2 }`
24+
- `{ [1] = "one"; [2] = "two" }`
2425
- Map update/merge is available directly in literals:
2526
- `{ ["a"] = 1; ..tail }`
2627
- Function types use arrow syntax:
@@ -55,6 +56,7 @@ let x =
5556
- Typed decoding workflows use `typeof Name` tokens with host externs.
5657
- Capability maps can use `nameof identifier` for stable script-side function keys.
5758
- Capability maps use string keys in map literals (`[expr]` where `expr : string`).
59+
- Maps also support integer keys (`[expr]` where `expr : int`) for numeric indexing scenarios.
5860

5961
## Formatting and layout choices
6062
- `match` case columns align.

docs/supported-types.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ This document specifies the value and type system used by the interpreter.
1414
- List: `'a list`
1515
- Tuple: `(t1 * t2 * ...)`
1616
- Option: `'a option`
17-
- String-keyed map: `'a map`
17+
- Map (string-keyed alias): `'a map`
1818
- Record: structural record types
1919
- Discriminated union: named union types with cases
2020

2121
## Map literals
2222
- Native map literal syntax:
2323
- empty: `{}`
2424
- populated: `{ ["a"] = 1; ["b"] = 2 }`
25+
- int keys: `{ [1] = "one"; [2] = "two" }`
2526
- spread update: `{ ["a"] = 1; ..tail }`
2627
- multiline entries are supported in an indented block.
27-
- Keys are bracketed expressions and must have type `string` (for example `{ [keyExpr] = value }`).
28+
- Keys are bracketed expressions and must have type `string` or `int` (for example `{ [keyExpr] = value }`).
2829
- Record literals and map literals share `{ ... }` braces:
2930
- map entries use `[expr] = value`
3031
- record entries use `Field = value`
@@ -76,7 +77,7 @@ This document specifies the value and type system used by the interpreter.
7677
- `VUnit`, `VInt`, `VFloat`, `VBool`, `VString`
7778
- `VList`, `VTuple`
7879
- `VRecord`
79-
- `VStringMap`
80+
- `VMap`
8081
- `VOption`
8182
- `VUnionCase`, `VUnionCtor`
8283
- `VClosure`

docs/syntax-and-indentation.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This document describes the concrete syntax accepted by the interpreter and the
3838
- record patterns in cases: `{ Field = pattern; ... }`
3939
- map patterns in cases support keyed lookup and extraction:
4040
- keyed lookup: `{ ["key"] = v }`
41+
- int-key lookup: `{ [1] = v }`
4142
- multiple keys: `{ ["a"] = x; ["b"] = y }`
4243
- optional tail capture: `{ ["a"] = x; ..tail }`
4344
- dynamic extraction (preserved): `{ [k] = v; ..tail }`
@@ -57,11 +58,12 @@ This document describes the concrete syntax accepted by the interpreter and the
5758
- Maps:
5859
- empty `{}`
5960
- literal `{ ["a"] = 1; ["b"] = 2 }`
61+
- int-key literal `{ [1] = "one"; [2] = "two" }`
6062
- update/merge with spread `{ ["a"] = 1; ..tail }`
6163
- map entries always use bracketed keys (`[expr] = value`)
6264
- record entries use field assignments (`Field = value`)
6365
- when braces are empty (`{}`), the literal is a map
64-
- keys are bracketed expressions (`[expr]`) and must infer to `string`
66+
- keys are bracketed expressions (`[expr]`) and must infer to `string` or `int`
6567
- if entries start on the next line, `{` must be on its own line
6668
- multiline example:
6769
```fsharp

samples/map-matching.fss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ let values =
44
["beta"] = 20
55
["gamma"] = 30 }
66

7+
let indexed =
8+
{ [1] = "one"
9+
[2] = "two"
10+
[3] = "three" }
11+
712
match values with
813
| { ["alpha"] = v1; ..tail } ->
914
print $"alpha={v1}; tail={tail}"
@@ -18,3 +23,8 @@ match values with
1823
| { [k] = v; ..tail } ->
1924
print $"dynamic extraction: {k} = {v}, tail={tail}"
2025
| _ -> print "no dynamic match"
26+
27+
match indexed with
28+
| { [1] = first; [2] = second; ..tail } ->
29+
print $"int-key match: first={first}, second={second}, tail={tail}"
30+
| _ -> print "no int-key match"

samples/types-showcase.fss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ type Subscription =
2222
let scores =
2323
{ ["math"] = 20; ["science"] = 18 }
2424

25+
let statusByCode =
26+
{ [200] = "ok"; [404] = "not-found" }
27+
2528

2629
let user = { Id = 1; Name = "Ada"; Tags = ["engineer"; "writer"]; Address = Some { City = "London"; Zip = 12345 }; Scores = scores; Pair = (7, "seven") }
2730
let subscription = Premium 12
@@ -75,6 +78,7 @@ let monthlyPrice =
7578
| Premium amount -> amount
7679

7780
let missing = "missing"
81+
let unknown = "unknown"
7882

7983
print $"User: {updated.Name}"
8084
print $"City: {city}"
@@ -90,6 +94,7 @@ print $"Subscription price: {monthlyPrice}"
9094
print $"Structural address from literal: {format_address parisAddress}"
9195
print $"Structural address from OfficeAddress: {format_address officeAddress}"
9296
print $"Capability for format_address: {Map.tryGet formatAddressKey capabilities |> Option.defaultValue missing}"
97+
print $"HTTP 200: {statusByCode[200] |> Option.defaultValue unknown}"
9398

9499

95100
updated.Tags |> List.iter print

0 commit comments

Comments
 (0)