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 entries
Map 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.

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:

LiteralKeysShapeUse
{ name: "Tim", age: 30 }identifiersfixed at compile timeanonymous struct — typed fields, .name access
{ "X-Test": "hi" }string literalsdynamicMap<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).