Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library · Collections

    Map.get for K, V

    Look up a key and represent its absence with Option.

    get(k: K) -> Option<V>
    Receiver
    Map<K, V>
    Minimum arity
    1
    Maximum arity
    1

    Declaration and parameters

    Link

    Availability

    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

    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.

    Result

    The declared result type is Option<V>.

    Effects

    Failures

    Result and effects

    Link

    A 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

    Link

    Returning Option gives Map lookup the same absence and fallback rules as other optional values.

    Related entries

    Link

    Collections

    Use Map.get with optional fallback

    Link
    Ruletopaz.stdlib.contract.map-get-fallback.rule

    Use 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

    Evaluate a fallback for a missing key

    Link
    main.tpzEvaluate a fallback for a missing keyOpen in Playground
    1. import std.io
    2. function fallback() -> int {
    3. io.writeLine("fallback")
    4. 9
    5. }
    6. let values: Map<string, int> = map { "present": 7 }
    7. let present = values.get("present") ?? fallback()
    8. io.writeLine("{present}")
    9. let missing = values.get("missing") ?? fallback()
    10. io.writeLine("{missing}")
    Expected outputexit 0
    7
    fallback
    9
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. 7
    2. fallback
    3. 9
    Source evidence

    Reject the removed Map.getOr member

    Link
    main.tpzReject the removed Map.getOr memberOpen in Playground
    1. let m: Map<string, int> = map { "present": 7 }
    2. m.getOr("present", 9)
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5006 stdlib-removed-member at main.tpz:2:3
    Source evidence

    Supply the default with get and ??

    Link
    main.tpzSupply the default with get and ??Open in Playground
    1. let m: Map<string, int> = map { "present": 7 }
    2. m.get("present") ?? 9
    Expected outputexit 0
    No stdout.
    $ topaz check main.tpz --format json · target product 5.21.3
    Expected result

    Expected result

    Checking accepts the program. Exit code 0.

    Source evidence

    Preserve eager default effects with ordered bindings

    Link
    main.tpzPreserve eager default effects with ordered bindingsOpen in Playground
    1. import std.io
    2. function makeMap() -> Map<string, int> {
    3. io.writeLine("receiver")
    4. map { "present": 7 }
    5. }
    6. function makeKey() -> string {
    7. io.writeLine("key")
    8. "present"
    9. }
    10. function makeFallback() -> int {
    11. io.writeLine("fallback")
    12. 9
    13. }
    14. let receiver = makeMap()
    15. let key = makeKey()
    16. let fallback = makeFallback()
    17. let value = receiver.get(key) ?? fallback
    18. io.writeLine("{value}")
    Expected outputexit 0
    receiver
    key
    fallback
    7
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. receiver
    2. key
    3. fallback
    4. 7
    Source evidence

    Diagnostics

    Link

    Unknown member or field

    Menu

    Download