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)
표준 출력 없음.기대 결과
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 ()}
표준 출력 없음.기대 결과
Reject Map.isEmpty
링크function inspect(value: Map<string, int>) -> () {let result = value.isEmpty()return ()}
표준 출력 없음.기대 결과
Reject Set.isEmpty
링크function inspect(value: Set<int>) -> () {let result = value.isEmpty()return ()}
표준 출력 없음.기대 결과
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)
표준 출력 없음.기대 결과
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)
표준 출력 없음.기대 결과
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