Prerequisite
LinkUse 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
LinkSave the source as data-. Ticket has named fields and a Status value. Construct a variant with Status.. Match it with case Open().
2. Check the program
LinkCheck the ticket program
Linkimport std.ioenum Status { Open, Closed }record Ticket { title: string, status: Status }function describe(ticket: Ticket) -> string {let state = match (ticket.status) {case Open() => "open"case Closed() => "closed"}return "{ticket.title}: {state}"}let first = Ticket { title: "Docs", status: Status.Open() }let second = Ticket { title: "Tests", status: Status.Closed() }io.writeLine(describe(first))io.writeLine(describe(second))
data-and-patterns.tpz: types-ok (2 modules) data-and-patterns.tpz: resolve-ok (2 modules)
Expected result
Expected result
Checking accepts the program. Exit code 0.
Observed assertions · passed · 5.21.3
Exit code 0.
data-and-patterns.tpz: types-ok (2 modules)data-and-patterns.tpz: resolve-ok (2 modules)
No stderr.
- Passed · expected 0; observed 0
Source evidence
- parseEnum; parseRecord at
b48527d0a174· 294-411 - bindNamedPatternType at
b48527d0a174· 542-584 - classify at
b48527d0a174· 7-14 - Point construction at
b48527d0a174· 1-4 - Color cases at
b48527d0a174· 1-6
Expect exit 0. If checking fails, follow the reported diagnostic, correct the source, and check again.
3. Run both cases
LinkPrint both ticket states
Linkimport std.ioenum Status { Open, Closed }record Ticket { title: string, status: Status }function describe(ticket: Ticket) -> string {let state = match (ticket.status) {case Open() => "open"case Closed() => "closed"}return "{ticket.title}: {state}"}let first = Ticket { title: "Docs", status: Status.Open() }let second = Ticket { title: "Tests", status: Status.Closed() }io.writeLine(describe(first))io.writeLine(describe(second))
Docs: open Tests: closed
Expected result
Expected result
Execution completes. Exit code 0.
Docs: openTests: closed
Observed assertions · passed · 5.21.3
Exit code 0.
Docs: openTests: closed
No stderr.
- Passed · expected 0; observed 0
- Passed · stdout matches the authored expectation
- Passed · stderr matches the authored expectation
Source evidence
- parseEnum; parseRecord at
b48527d0a174· 294-411 - bindNamedPatternType at
b48527d0a174· 542-584 - classify at
b48527d0a174· 7-14 - Point construction at
b48527d0a174· 1-4 - Color cases at
b48527d0a174· 1-6
The two calls exercise both cases used by describe.
Verification
LinkPublic 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
LinkContinue with Generic functions and protocols. For parameter and return syntax, use Functions and generics.