Built-in types
СсылкаArray, Map, and Set values are available without a module import. Their members provide collection operations alongside the language rules for literals, membership, and iteration. Assignment and transfer follow collection value semantics. Map and Set observations follow insertion order.
In map, filter and reduce, callback propagation returns from the callback itself. The surrounding function continues after the standard function returns. Mapping a callback that returns Result produces an array of Results. A filter callback must return bool. A lambda using ? must return Result or Option and therefore fails the existing callback type check in filter. A reduce callback still returns the accumulator type required by that call.
Why
СсылкаArray preserves element order, Set models membership, and Map associates keys with values. Shared lookup and observation rules make their operations consistent.
Availability
СсылкаThese types are provided by the language. No module import is required. The declarations below describe the standard operations on built-in values.
Public API
СсылкаPending contracts
СсылкаUse Map.get with optional fallback
Ссылка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.
Причина
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.
Evaluate a fallback for a missing key
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
7fallback9
Reject the removed Map.getOr member
Ссылкаlet m: Map<string, int> = map { "present": 7 }m.getOr("present", 9)
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:2:3
Supply the default with get and ??
Ссылкаlet m: Map<string, int> = map { "present": 7 }m.get("present") ?? 9
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Проверка принимает программу. Код завершения 0.
Preserve eager default effects with ordered bindings
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
receiverkeyfallback7
Diagnostics
СсылкаTest collection emptiness with length
СсылкаTest collection emptiness with length
Test whether a Bytes, Map, or Set value is empty by comparing its length property with zero. The comparison is true exactly when the collection has no bytes, entries, or elements.
Bytes, Map, and Set do not expose an isEmpty member. Read the length property and use the ordinary equality operator.
Причина
A collection already exposes its count through length. Comparing that value with zero expresses emptiness for every collection without another query name or a type-specific method.
Test empty and nonempty maps with length
Ссылкаimport std.iolet empty: Map<string, int> = map {}let filled = map { "one": 1 }io.writeLine("{empty.length == 0}:{filled.length == 0}")
true:false
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
true:false
Test empty and nonempty sets with length
Ссылкаimport std.iolet empty: Set<int> = set {}let filled = set { 1 }io.writeLine("{empty.length == 0}:{filled.length == 0}")
true:false
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
true:false
Test empty and nonempty byte sequences with length
Ссылкаimport std.bytes { Bytes }import std.testimport std.iolet empty = Bytes.empty()let filled = test.assertOk(Bytes.fromArray([1]))io.writeLine("{empty.length == 0}:{filled.length == 0}")
true:false
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
true:false
Reject Bytes.isEmpty
Ссылкаimport std.bytes { Bytes }function inspect(value: Bytes) -> () {let result = value.isEmpty()return ()}
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:3:22
Reject Map.isEmpty
Ссылкаfunction inspect(value: Map<string, int>) -> () {let result = value.isEmpty()return ()}
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:2:22
Reject Set.isEmpty
Ссылкаfunction inspect(value: Set<int>) -> () {let result = value.isEmpty()return ()}
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:2:22
Use in for element membership
СсылкаUse in for element membership
Use x in values for membership in Array<T>, Set<T>, and Range<int>. The element must be compatible with the collection element type. Range membership checks its bounds and step alignment.
Set.contains is absent. Array does not declare a contains member. Use the membership operator with either collection.
string.contains(sub) remains a substring query and returns bool. It searches for the contiguous text sub. Strings do not become element-membership operands.
Map.containsKey(k) remains a query for key presence. A Map is not an element-membership operand. Key presence does not depend on the value stored for that key.
Причина
One membership operator works across arrays, sets, and ranges. Substring search examines contiguous text, and keyed lookup addresses associations. These queries retain their distinct argument and result contracts.
Query membership in arrays, sets, and ranges
Ссылкаimport std.iolet values = [1, 2, 3]let members = set { 1, 3 }let odd = 1..<6 by 2io.writeLine("{2 in values}")io.writeLine("{2 in members}")io.writeLine("{3 in members}")io.writeLine("{5 in odd}")io.writeLine("{4 in odd}")
true
false
true
true
false
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
truefalsetruetruefalse
Reject the removed Set.contains member
Ссылкаlet s: Set<int> = set { 1, 2 }s.contains(1)
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:2:3
Express set membership with in
Ссылкаlet s: Set<int> = set { 1, 2 }1 in s
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Проверка принимает программу. Код завершения 0.
Reject an undeclared Array.contains member
Ссылкаlet a: Array<int> = [1, 2]a.contains(1)
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения check. Ненулевой код завершения.
stdlib-removed-member at
main.tpz:2:3
Keep substring and key-presence queries explicit
Ссылкаimport std.iolet substring = "Topaz".contains("opa")let separated = "Topaz".contains("Tp")let values: Map<string, int> = map { "key": 1 }let present = values.containsKey("key")let absent = values.containsKey("other")io.writeLine("{substring}")io.writeLine("{separated}")io.writeLine("{present}")io.writeLine("{absent}")
true
false
true
false
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
truefalsetruefalse