Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

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

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

    Tour

    Data and patterns

    Build two ticket values and use an enum pattern to print their states.

    Prerequisite

    Link

    Use Topaz 5.21.3 with language mode 5.21 and the SELF compiler. Complete First checked and executed program first.

    1. Build a value and handle its cases

    Link

    Save the source as data-and-patterns.tpz. Ticket has named fields and a Status value. Construct a variant with Status.Open(). Match it with case Open().

    2. Check the program

    Link

    Check the ticket program

    Link
    data-and-patterns.tpzCheck the ticket programOpen in Playground
    1. import std.io
    2. enum Status { Open, Closed }
    3. record Ticket { title: string, status: Status }
    4. function describe(ticket: Ticket) -> string {
    5. let state = match (ticket.status) {
    6. case Open() => "open"
    7. case Closed() => "closed"
    8. }
    9. return "{ticket.title}: {state}"
    10. }
    11. let first = Ticket { title: "Docs", status: Status.Open() }
    12. let second = Ticket { title: "Tests", status: Status.Closed() }
    13. io.writeLine(describe(first))
    14. io.writeLine(describe(second))
    Recorded outputexit 0
    data-and-patterns.tpz: types-ok (2 modules)
    data-and-patterns.tpz: resolve-ok (2 modules)
    
    $ topaz check data-and-patterns.tpz --compiler self · recorded with topaz 5.21.3
    Expected result

    Expected result

    Checking accepts the program. Exit code 0.

    Observed assertions · passed · 5.21.3

    Exit code 0.

    Observed stdout
    1. data-and-patterns.tpz: types-ok (2 modules)
    2. data-and-patterns.tpz: resolve-ok (2 modules)

    No stderr.

    • Passed · expected 0; observed 0
    Source evidence

    Expect exit 0. If checking fails, follow the reported diagnostic, correct the source, and check again.

    3. Run both cases

    Link

    Print both ticket states

    Link
    data-and-patterns.tpzPrint both ticket statesOpen in Playground
    1. import std.io
    2. enum Status { Open, Closed }
    3. record Ticket { title: string, status: Status }
    4. function describe(ticket: Ticket) -> string {
    5. let state = match (ticket.status) {
    6. case Open() => "open"
    7. case Closed() => "closed"
    8. }
    9. return "{ticket.title}: {state}"
    10. }
    11. let first = Ticket { title: "Docs", status: Status.Open() }
    12. let second = Ticket { title: "Tests", status: Status.Closed() }
    13. io.writeLine(describe(first))
    14. io.writeLine(describe(second))
    Recorded outputexit 0
    Docs: open
    Tests: closed
    
    $ topaz run data-and-patterns.tpz --compiler self · recorded with topaz 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. Docs: open
    2. Tests: closed
    Observed assertions · passed · 5.21.3

    Exit code 0.

    Observed stdout
    1. Docs: open
    2. Tests: closed

    No stderr.

    • Passed · expected 0; observed 0
    • Passed · stdout matches the authored expectation
    • Passed · stderr matches the authored expectation
    Source evidence

    The two calls exercise both cases used by describe.

    Verification

    Link

    Public Topaz 5.21.3 checked and ran this program successfully in Docs run 42224592687, attempt 2. The recorded exit codes and output were compared with this page's current product assertions. The fixture files, invocation and expected output were unchanged.

    Records, enums and newtypes defines the declarations used here. Pattern matching defines variant patterns, case selection and match coverage.

    Next steps

    Link

    Functions and generics

    check

    std.io.writeLine

    Continue with Generic functions and protocols. For parameter and return syntax, use Functions and generics.

    Menu

    Download