get(k: K) - > Option<V>- Module
- Collections
- Receiver
- Map<K, V>
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: yes
These types are provided by the language. No module import is required.
The declarations below describe the standard operations on built-in values.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
271329b482bc· 882 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
271329b482bc· 326 - compiler/crates/topaz_value/src/value/members.rs at
271329b482bc· 1017
Receiver
Map<K, V>
Parameter constraints
Map<K, V>.get(k) accepts a key of type K and returns Option<V>. A present key produces Some(value), and an absent key produces None(). Lookup does not insert an entry.
Use map.get(key) ?? fallback to supply a value for an absent key. Evaluate the receiver and key once, then perform the lookup once. A present value skips the fallback. An absent key evaluates the fallback once.
The fallback must be compatible with V. A stored Option or nullable value remains the payload of a present key. Its inner None() or null does not make that key absent.
Map.getOr is absent. Its former eager default evaluation is expressed with ordinary bindings when those effects are needed.
To preserve eager effects, bind the receiver, key, and default in that order. Perform receiver.get(key) ?? default after those bindings. This preserves default evaluation before lookup, including effects that change the map.
Related rule: Use Map.get with optional fallbackResult
The declared result type is Option<V>.
Effects
Failures
Result and effects
LinkA present key produces Some(value), and an absent key produces None(). The lookup does not insert an entry. Apply ?? to supply a fallback when the key is absent.
Why
LinkReturning Option gives Map lookup the same absence and fallback rules as other optional values.
Related entries
LinkUse Map.get with optional fallback
LinkUse Map.get with optional fallback
Map<K, V>.get(k) accepts a key of type K and returns Option<V>. A present key produces Some(value), and an absent key produces None(). Lookup does not insert an entry.
Use map.get(key) ?? fallback to supply a value for an absent key. Evaluate the receiver and key once, then perform the lookup once. A present value skips the fallback. An absent key evaluates the fallback once.
The fallback must be compatible with V. A stored Option or nullable value remains the payload of a present key. Its inner None() or null does not make that key absent.
Map.getOr is absent. Its former eager default evaluation is expressed with ordinary bindings when those effects are needed.
To preserve eager effects, bind the receiver, key, and default in that order. Perform receiver.get(key) ?? default after those bindings. This preserves default evaluation before lookup, including effects that change the map.
Why
Map lookup represents absence with Option. The shared coalescing operator supplies defaults with one evaluation rule. Explicit bindings preserve eager effects without a second lookup API.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
271329b482bc· 871 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
271329b482bc· 326 - compiler/crates/topaz_value/src/value/members.rs at
271329b482bc· 1017 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 483 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 950 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1387 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1511 -
topaz.stdlib.contract.map-get-fallbackatworking-tree - topaz.decision.map-get-fallback.1 at
topaz.decisi
Evaluate a fallback for a missing key
Linkimport std.iofunction fallback() -> int {io.writeLine("fallback")9}let values: Map<string, int> = map { "present": 7 }let present = values.get("present") ?? fallback()io.writeLine("{present}")let missing = values.get("missing") ?? fallback()io.writeLine("{missing}")
7 fallback 9
Expected result
Expected result
Execution completes. Exit code 0.
7fallback9
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
271329b482bc· 871 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
271329b482bc· 326 - compiler/crates/topaz_value/src/value/members.rs at
271329b482bc· 1017 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 483 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 950 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1387 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1511 -
topaz.stdlib.contract.map-get-fallbackatworking-tree - topaz.decision.map-get-fallback.1 at
topaz.decisi
Reject the removed Map.getOr member
Linklet m: Map<string, int> = map { "present": 7 }m.getOr("present", 9)
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
271329b482bc· 871 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
271329b482bc· 326 - compiler/crates/topaz_value/src/value/members.rs at
271329b482bc· 1017 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 483 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 950 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1387 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1511 -
topaz.stdlib.contract.map-get-fallbackatworking-tree - topaz.decision.map-get-fallback.1 at
topaz.decisi
Supply the default with get and ??
Linklet m: Map<string, int> = map { "present": 7 }m.get("present") ?? 9
No stdout.
Expected result
Expected result
Checking accepts the program. Exit code 0.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
271329b482bc· 871 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
271329b482bc· 326 - compiler/crates/topaz_value/src/value/members.rs at
271329b482bc· 1017 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 483 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 950 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1387 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1511 -
topaz.stdlib.contract.map-get-fallbackatworking-tree - topaz.decision.map-get-fallback.1 at
topaz.decisi
Preserve eager default effects with ordered bindings
Linkimport std.iofunction makeMap() -> Map<string, int> {io.writeLine("receiver")map { "present": 7 }}function makeKey() -> string {io.writeLine("key")"present"}function makeFallback() -> int {io.writeLine("fallback")9}let receiver = makeMap()let key = makeKey()let fallback = makeFallback()let value = receiver.get(key) ?? fallbackio.writeLine("{value}")
receiver key fallback 7
Expected result
Expected result
Execution completes. Exit code 0.
receiverkeyfallback7
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
271329b482bc· 871 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
271329b482bc· 326 - compiler/crates/topaz_value/src/value/members.rs at
271329b482bc· 1017 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 483 - compiler/crates/topaz_self_frontend/topaz/emitter/emit.tpz at
271329b482bc· 950 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1387 - compiler/crates/topaz_self_frontend/topaz/checker/expression_core.tpz at
271329b482bc· 1511 -
topaz.stdlib.contract.map-get-fallbackatworking-tree - topaz.decision.map-get-fallback.1 at
topaz.decisi