渲染zh-Hans

映射

Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.

Generic key/value map type.

Aliases: map, dictionary

Syntax: 映射<K,V>

Category#

collection

Examples#

radix/corpus/tabula/methodi-accessus.fab (canonical · keyword)#

Generic key/value map type.

# =============================================================================
# 映射 — Generic key/value map type.
# =============================================================================
#
# What this teaches:
#   • Map access methods — `accipe`, `ponde`, `habet`, `dele`, `longitudo`, `空集`
#   • Null-safe access — using `vel` to supply defaults when a key is absent
#
# Common mistakes:
#   • attempting to access a missing key without a default and getting a ∪ 空类型 value — use vel for fallback
#
# See also: 列表, 集合, ∷, 遍历
# =============================================================================




# 映射<K,V> access and mutation intrinsics
#
# puncta.put(k, v) | puncta.get(k) | puncta.has(k)   -- get/put
# puncta.delete(k) | puncta.length() | puncta.is_empty()      -- delete/size
#
# GRAMMAR:
#   mapMethod :← expr '.' ('pone' | 'accipe' | 'habet' | 'dele'
#                         | 'longitudo' | '空集') '(' args? ')'
#
# EXPECTED OUTPUT:
#   No pinned stdout — smoke asserts exit 0; 映射/tabula.fab carries scalar contract.
#
# BACKEND: Wasm tier floor FrontendAnalyzed (method surface not yet Runnable;
# whitelist: 映射/methodi-accessus.fab).

main {
    var map<string, int> puncta ← empty
    puncta.put("aelia", 95)
    puncta.put("balbus", 87)
    const int ∪ none aelia ← puncta.get("aelia")
    const int ∪ none absens ← puncta.get("carus")
    const int carus ← puncta.get("carus") coalesce 0
    const bool habetbalbum ← puncta.has("balbus")
    const bool remotus ← puncta.delete("balbus")
    const int longitudo ← puncta.length()
    const bool vacuaest ← puncta.is_empty()
    print aelia, absens, carus, habetbalbum, remotus, longitudo, vacuaest
}

Expected output:

95 nihil 0 verum verum 1 falsum

radix/corpus/tabula/tabula.fab (canonical · keyword)#

Generic key/value map type.

# =============================================================================
# 映射 — Generic key/value map type.
# =============================================================================
#
# What this teaches:
#   • Map declaration — `映射<K,V>` with bracket syntax for keyed access
#   • Map literals — empty map via `空集`, then populating with index assignment
#
# Common mistakes:
#   • using bracket syntax with a key type that doesn't match the 映射 declaration, or inserting duplicate keys
#
# See also: 列表, 集合, ∷, 遍历
# =============================================================================




# 映射<K,V> declaration, 空集, and keyed lookup
#
# 变量 映射<K,V> 名称 ← 空集   -- typed empty map (forma innata)
# 名称[key] ← value                -- index assignment
# 名称[key]                        -- keyed accipe
# nomen.length()                 -- entry count
#
# GRAMMAR:
#   mapDecl :← '变量' '映射<' type ',' type '>' ident '←' expr
#   mapAcc  :← expr '[' expr ']'
#
# EXPECTED OUTPUT:
#   longitudo() plus keyed lookups for alpha/beta/gamma (tabula.expected).

main {
    var map<string, int> puncta ← empty
    puncta["alpha"] ← 1
    puncta["beta"] ← 2
    puncta["gamma"] ← 3
    print puncta.length()
    print puncta["alpha"]
    print puncta["beta"]
    print puncta["gamma"]
}

Expected output:

3
1
2
3

radix/corpus/tabula/key-not-hashable.fab (supporting · reject)#

Map keys and set elements must be hashable: floats and mutable collections are rejected.

# =============================================================================
# key-not-hashable — map keys and set elements must be hashable.
# =============================================================================
#
# Decline rows (reject reason in each comment):
#   1. `map<f64, string>` — a float key (NaN is not equal to itself):
#      SEM010 tabula_key_not_hashable
#   2. `set<list<int>>` — a mutable collection as a set element:
#      SEM010 copia_element_not_hashable
#
# What is hashable: text, integers, booleans, and tuples or classes whose
# every part is hashable.
#
# Common mistakes:
#   • keying by a float measurement — round to an integer unit first
#
# See also: 映射, 集合, 元组
# =============================================================================

fn weights(map<f64, string> m) → int {
    return m.length()
}

fn members(set<list<int>> s) → int {
    return s.length()
}

Expected: compilation rejects this example.