Skip to content

HashMap

Key-value store with string keys.

Methods

MethodReturnsDescription
HashMap.new()mapCreate empty map
set(key, val)Set value (string)
get(key)stringGet value ("" if missing)
has(key)boolKey exists
remove(key)Delete entry
len()intEntry count
keys()[string]Array of all keys
values()[string]Array of all values
clear()Remove all entries

Examples

wyn
var m = HashMap.new()
m.set("name", "Wyn")
m.set("version", "2")

println(m.get("name"))                  // Wyn
println(m.has("name").to_string())      // true
println(m.len().to_string())            // 2

Iteration

wyn
var ks = m.keys()
for k in ks {
    println("${k}: ${m.get(k)}")
}
// name: Wyn
// version: 2

Values

wyn
var vs = m.values()
for v in vs {
    println(v)
}

Global Access

HashMap variables can be accessed from functions:

wyn
var config = HashMap.new()

fn setup() {
    config.set("host", "localhost")
    config.set("port", "8080")
}

fn get_url() -> string {
    return config.get("host") + ":" + config.get("port")
}

setup()
println(get_url())  // localhost:8080

MIT License