Maps
Map is a string-keyed, string-valued map (Map<string,string>) — the backing
store for dynamic key/value data such as HTTP headers and config. Keys are
looked up by exact string match.
Creating a map
A map literal uses brace syntax with string-literal keys:
Map headers = { "Content-Type": "application/json", "X-Trace": "42" }The string keys are what distinguish a map literal from an
anonymous struct literal ({ name: "Tim" }, which
has identifier keys). An empty or dynamically-built map uses the constructor:
Map m = Map()
m.set("host", "localhost") Methods
m.get(key: string) : string // value, or "" if absent
m.set(key: string, v: string) : Map // insert/replace; returns m (chainable)
m.has(key: string) : bool // is the key present?
m.remove(key: string) : void // delete a key (no-op if absent)
m.keys() : string[] // all keys, insertion order
m.values() : string[] // all values, insertion order
m.size() : int // number of entriesMap m = { "a": "1", "b": "2" }
m.set("a", "10").set("c", "3") // set replaces "a", adds "c", and chains
sys.output(m.get("a")) // "10"
sys.output(m.size().toString()) // 3
for (string k in m.keys()) {
sys.output(k + " = " + m.get(k))
}get returns "" for an absent key, so use has when you need to tell an
absent key from one whose value is the empty string.
To print or log a map, serialise it: sys.output(json.from(m)) writes
{"a":"10","b":"2","c":"3"} for the map above (a Map encodes every value as
a JSON string). json.parse<Map>(text) reads an object back, and is the way to
accept JSON with keys you do not know in advance.
Typed maps
map[string, T] is a string-keyed map with values of any one type T
(map[string, int], map[string, Pet[]]). Declare one without an initializer
for an empty map, or decode JSON into it; there is no literal form ({ ... }
is always a Map<string,string>).
map[string, int] stock
stock.set("apple", 3).set("pear", 5)
map[string, int] parsed = json.parse<map[string, int]>('{"p":1,"q":2}')m.get(key: string) : T // throws Error("typed dictionary: missing key") if absent
m.set(key: string, v: T) : map[string, T] // insert/replace; returns m (chainable)
m.has(key: string) : bool
m.remove(key: string) : void
m.keys() : string[] // insertion order
m.size() : intUnlike Map, get throws for an absent key, so check has first. There is
no values(). Keys must be strings (another key type is a compile error at the
first method call). list.groupBy(f) returns a map[string, T[]], and
json.from writes a typed map as a JSON object. Typed maps are returned as
copies in the same cases as Map.
Returning a map
A function that returns a map it did not build itself — namespace state, a
parameter, a field, or the receiver a chained .set returns — hands the caller
a copy, so callers cannot change priv state through a getter. A map the
function creates ({ ... }, Map(), another call’s result) is returned as is.
ns settings {
priv Map headers = { "accept": "json" }
fn all(): Map { return headers } // caller gets a copy
}
Map mine = settings.all()
mine.set("accept", "xml") // settings' headers are unchanged Maps as HTTP headers
Every http client verb accepts a Map<string,string> wherever it accepts a
string[] of headers — the map is serialised to "Name: value" lines for you:
http.getSync(url, { "Authorization": "Bearer abc123", "Accept": "application/json" })
http.postSync(url, body, { "Content-Type": "application/json", "X-Trace": "42" })See http for the full client/server surface.
Maps vs anonymous structs
Both use braces, but they are different constructs:
| Literal | Keys | Shape | Use |
|---|---|---|---|
{ name: "Tim", age: 30 } |
identifiers | fixed at compile time | anonymous struct — typed fields, .name access |
{ "X-Test": "hi" } |
string literals | dynamic | Map<string,string> — .get/.set by runtime key |
A struct’s fields are frozen when you write it; a map’s keys are runtime values,
and only a map can hold keys that aren’t valid identifiers (e.g. Content-Type).