Map.get
func Map.get[K, V](m: Map[K, V], k: K): V | nullMap.get returns the value m holds for k, or null when k is missing. It is the same as m[k]; use Map.has when V itself includes null.
Details
- It never panics and never adds an entry.
- Keys compare with
==, so a record, an array, or a union value works as a key. - A record, array, map, or set it returns is the value stored in
m, not a copy.
Basic examples
Read the value for a key:
let ages = Map{ ada: 36, linus: 54 }
Map.get(ages, "ada") // 36A missing key gives null:
let ages = Map{ ada: 36, linus: 54 }
Map.get(ages, "grace") // nullGive a missing key a default with ??:
let ages = Map{ ada: 36, linus: 54 }
ages.get("grace") ?? 0 // 0Scope
An empty map has no entries. Annotate it so that K and V are known:
let empty: Map[string, int] = Map{}
empty.get("a") // nullRecords and arrays are keys by value, so a new record equal to the stored key finds its entry:
type Point = { x: int, y: int }
let labels = Map{ Point{ x: 0, y: 0 }: "origin" }
labels.get(Point{ x: 0, y: 0 }) // "origin"
labels.get(Point{ x: 1, y: 0 }) // null
let cells = Map{ [0, 1]: "b1" }
cells.get([0, 1]) // "b1"Keys of a union type tell their members apart, so 1 and "1" are two keys:
type Id = int | string
let ids: Map[Id, string] = Map{ 1: "number", "1": "text" }
ids.get(1) // "number"
ids.get("1") // "text"Look up a key of a named primitive type with a value of that type:
type UserId = string
let owners = Map{ UserId("u-1"): "Ann" }
owners.get(UserId("u-1")) // "Ann"String keys compare by code points. A precomposed é and an e followed by a combining accent look the same but are two keys:
let words = Map{ "café": 1 }
words.get("café") // 1
words.get("cafe\u{301}") // nullCall it through . or in a pipe:
let ages = Map{ ada: 36, linus: 54 }
ages.get("linus") // 54
ages | Map.get("ada") // 36Applications
Count words, starting each count at 0 with ??:
let counts: Map[string, int] = Map{}
for word in "to be or not to be".split(" ") {
counts[word] = (counts.get(word) ?? 0) + 1
}
println(counts)Map{ "to": 2, "be": 2, "or": 1, "not": 1 }Return early from a function when a key is missing, with ?? return:
type User = { name: string, email: string }
let users = Map{ "u-1": User{ name: "Ann", email: "ann@example.com" } }
func emailOf(id: string): string {
let user = users.get(id) ?? return "unknown user ${id}"
user.email
}
println(emailOf("u-1"))
println(emailOf("u-9"))ann@example.com
unknown user u-9Remember results that are slow to compute:
let cache: Map[int, int] = Map{}
func fib(n: int): int {
if n < 2 {
return n
}
let known = cache.get(n)
if known != null {
return known
}
let result = fib(n - 1) + fib(n - 2)
cache[n] = result
result
}
println(fib(80))23416728348467685Group records by a field, adding to the array that Map.get returns:
type Task = { owner: string, title: string }
let tasks = [
Task{ owner: "ann", title: "docs" },
Task{ owner: "bob", title: "tests" },
Task{ owner: "ann", title: "release" },
]
let byOwner: Map[string, Array[string]] = Map{}
for task in tasks {
let titles = byOwner.get(task.owner)
if titles == null {
byOwner[task.owner] = [task.title]
} else {
titles.push(task.title)
}
}
println(byOwner)Map{ "ann": ["docs", "release"], "bob": ["tests"] }Properties and relations
Map.get is the same as indexing with m[k]:
let ages = Map{ ada: 36 }
assert(ages.get("ada") == ages["ada"])
assert(ages.get("grace") == ages["grace"])After Map.set, it returns the value just stored; after Map.remove, it returns null:
let ages = Map{ ada: 36 }
ages.set("ada", 37)
assert(ages.get("ada") == 37)
ages.remove("ada")
assert(ages.get("ada") == null)When V has no null, it returns null exactly when Map.has is false:
let ages = Map{ ada: 36 }
assert((ages.get("ada") != null) == ages.has("ada"))
assert((ages.get("grace") != null) == ages.has("grace"))It reads without changing the map, so a missing key is not added:
let ages = Map{ ada: 36 }
ages.get("grace")
assert(ages.size() == 1)Possible issues
The result may be null, so using it as a V without narrowing is a compile error:
let ages = Map{ ada: 36 }
println(ages.get("ada") + 1)
// error: + needs both operands of one type, got int | null and intUse ?? or a test against null first, as in the examples above.
When V includes null, a stored null and a missing key look the same. Use Map.has to tell them apart:
let scores: Map[string, int | null] = Map{ ann: null }
scores.get("ann") // null
scores.get("bob") // null
scores.has("ann") // true
scores.has("bob") // falseThe key must have the map's key type:
let ages = Map{ ada: 36 }
println(ages.get(1)) // error: cannot use int where string is expectedA key of a named primitive type needs that type; a plain string does not convert:
type UserId = string
let owners = Map{ UserId("u-1"): "Ann" }
println(owners.get("u-1")) // error: cannot use string where UserId is expectedA key mutated after it was inserted may no longer find its entry. Do not change a record or an array while it is a key; remove the entry, change the key, and insert it again.
See also
Map.has · Map.set · Map.remove · Map.keys · Map.values · Array.find · indexing · ?? operator · ?. operator
