Prerequisite
링크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
링크Save 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
링크Check the ticket program
링크import 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)
기대 결과
기대 결과
검사가 프로그램을 받아들입니다. 종료 코드 0.
관측한 단언 · 통과 · 5.21.3
종료 코드 0.
data-and-patterns.tpz: types-ok (2 modules)data-and-patterns.tpz: resolve-ok (2 modules)
표준 오류 출력 없음.
- 통과 · expected 0; observed 0
Expect exit 0. If checking fails, follow the reported diagnostic, correct the source, and check again.
3. Run both cases
링크Print both ticket states
링크import 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
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
Docs: openTests: closed
관측한 단언 · 통과 · 5.21.3
종료 코드 0.
Docs: openTests: closed
표준 오류 출력 없음.
- 통과 · expected 0; observed 0
- 통과 · stdout matches the authored expectation
- 통과 · stderr matches the authored expectation
The two calls exercise both cases used by describe.
Verification
링크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
링크Continue with Generic functions and protocols. For parameter and return syntax, use Functions and generics.