본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리

    컬렉션

    Look up and inspect built-in collection values.

    이 모듈의 API

    3개 API

    이름모듈
    Map.get →컬렉션 →
    Map.length →컬렉션 →
    Set.length →컬렉션 →

    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

    링크

    Map.get for K, V →

    Map.length for K, V →

    Set.length for T →

    Pending contracts

    링크

    Use Map.get with optional fallback

    링크
    규칙topaz.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.

    이유

    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

    링크
    main.tpzEvaluate a fallback for a missing key플레이그라운드 열기 →
    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}")
    기대 출력종료 코드 0
    7
    fallback
    9
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 7
    2. fallback
    3. 9

    Reject the removed Map.getOr member

    링크
    main.tpzReject the removed Map.getOr member플레이그라운드 열기 →
    1. let m: Map<string, int> = map { "present": 7 }
    2. m.getOr("present", 9)
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Supply the default with get and ??

    링크
    main.tpzSupply the default with get and ??플레이그라운드 열기 →
    1. let m: Map<string, int> = map { "present": 7 }
    2. m.get("present") ?? 9
    기대 출력종료 코드 0
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    검사가 프로그램을 받아들입니다. 종료 코드 0.

    Preserve eager default effects with ordered bindings

    링크
    main.tpzPreserve eager default effects with ordered bindings플레이그라운드 열기 →
    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}")
    기대 출력종료 코드 0
    receiver
    key
    fallback
    7
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. receiver
    2. key
    3. fallback
    4. 7

    Diagnostics

    링크

    Unknown member or field →

    Test collection emptiness with length

    링크
    규칙topaz.stdlib.contract.collection-emptiness.rule

    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

    링크
    main.tpzTest empty and nonempty maps with length플레이그라운드 열기 →
    1. import std.io
    2. let empty: Map<string, int> = map {}
    3. let filled = map { "one": 1 }
    4. io.writeLine("{empty.length == 0}:{filled.length == 0}")
    기대 출력종료 코드 0
    true:false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. true:false

    Test empty and nonempty sets with length

    링크
    main.tpzTest empty and nonempty sets with length플레이그라운드 열기 →
    1. import std.io
    2. let empty: Set<int> = set {}
    3. let filled = set { 1 }
    4. io.writeLine("{empty.length == 0}:{filled.length == 0}")
    기대 출력종료 코드 0
    true:false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. true:false

    Test empty and nonempty byte sequences with length

    링크
    main.tpzTest empty and nonempty byte sequences with length플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.test
    3. import std.io
    4. let empty = Bytes.empty()
    5. let filled = test.assertOk(Bytes.fromArray([1]))
    6. io.writeLine("{empty.length == 0}:{filled.length == 0}")
    기대 출력종료 코드 0
    true:false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. true:false

    Reject Bytes.isEmpty

    링크
    main.tpzReject Bytes.isEmpty플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. function inspect(value: Bytes) -> () {
    3. let result = value.isEmpty()
    4. return ()
    5. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Reject Map.isEmpty

    링크
    main.tpzReject Map.isEmpty플레이그라운드 열기 →
    1. function inspect(value: Map<string, int>) -> () {
    2. let result = value.isEmpty()
    3. return ()
    4. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Reject Set.isEmpty

    링크
    main.tpzReject Set.isEmpty플레이그라운드 열기 →
    1. function inspect(value: Set<int>) -> () {
    2. let result = value.isEmpty()
    3. return ()
    4. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Use in for element membership

    링크
    규칙topaz.stdlib.contract.membership.rule

    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

    링크
    main.tpzQuery membership in arrays, sets, and ranges플레이그라운드 열기 →
    1. import std.io
    2. let values = [1, 2, 3]
    3. let members = set { 1, 3 }
    4. let odd = 1..<6 by 2
    5. io.writeLine("{2 in values}")
    6. io.writeLine("{2 in members}")
    7. io.writeLine("{3 in members}")
    8. io.writeLine("{5 in odd}")
    9. io.writeLine("{4 in odd}")
    기대 출력종료 코드 0
    true
    false
    true
    true
    false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. true
    2. false
    3. true
    4. true
    5. false

    Reject the removed Set.contains member

    링크
    main.tpzReject the removed Set.contains member플레이그라운드 열기 →
    1. let s: Set<int> = set { 1, 2 }
    2. s.contains(1)
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Express set membership with in

    링크
    main.tpzExpress set membership with in플레이그라운드 열기 →
    1. let s: Set<int> = set { 1, 2 }
    2. 1 in s
    기대 출력종료 코드 0
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    검사가 프로그램을 받아들입니다. 종료 코드 0.

    Reject an undeclared Array.contains member

    링크
    main.tpzReject an undeclared Array.contains member플레이그라운드 열기 →
    1. let a: Array<int> = [1, 2]
    2. a.contains(1)
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Keep substring and key-presence queries explicit

    링크
    main.tpzKeep substring and key-presence queries explicit플레이그라운드 열기 →
    1. import std.io
    2. let substring = "Topaz".contains("opa")
    3. let separated = "Topaz".contains("Tp")
    4. let values: Map<string, int> = map { "key": 1 }
    5. let present = values.containsKey("key")
    6. let absent = values.containsKey("other")
    7. io.writeLine("{substring}")
    8. io.writeLine("{separated}")
    9. io.writeLine("{present}")
    10. io.writeLine("{absent}")
    기대 출력종료 코드 0
    true
    false
    true
    false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. true
    2. false
    3. true
    4. false

    메뉴

    다운로드 →