Skip to content
Topazdocs
6.05.21

Search documentation

Specification 6.0

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

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

    Download

    Library · Collections

    Set.length

    Inspect the declaration, parameters, and availability of this API.

    Set<R0>.length: int
    Receiver
    Set
    Minimum arity
    0
    Maximum arity
    0
    Language mode
    6.0
    Product
    Not released

    Declaration and parameters

    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.

    Receiver

    Set

    Parameter constraints

    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.

    Result

    The declared result type is int.

    Effects

    Failures

    Result and effects

    Select this field from a value of its named type without call parentheses.

    Runtime behavior, boundary inputs, effects, and failures remain Pending until their source contracts and public CLI observations are reviewed.

    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.

    Why

    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 lengthOpen in Playground
    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}")
    Expected outputExit code 0
    true:false
    
    $ topaz run main.tpz
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. true:false

    Test empty and nonempty sets with length

    main.tpzTest empty and nonempty sets with lengthOpen in Playground
    1. import std.io
    2. let empty: Set<int> = set {}
    3. let filled = set { 1 }
    4. io.writeLine("{empty.length == 0}:{filled.length == 0}")
    Expected outputExit code 0
    true:false
    
    $ topaz run main.tpz
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    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}")
    Expected outputExit code 0
    true:false
    
    $ topaz run main.tpz
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    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. }
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    Reject Map.isEmpty

    main.tpzReject Map.isEmpty
    1. function inspect(value: Map<string, int>) -> () {
    2. let result = value.isEmpty()
    3. return ()
    4. }
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    Reject Set.isEmpty

    main.tpzReject Set.isEmpty
    1. function inspect(value: Set<int>) -> () {
    2. let result = value.isEmpty()
    3. return ()
    4. }
    Expected outputNonzero exit
    No stdout.
    $ topaz check main.tpz
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    Diagnostics

    Unknown member or field →