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

문서 검색

명세 6.0

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

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

    투어

    데이터와 패턴

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

    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-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

    링크

    Check the ticket program

    링크
    data-and-patterns.tpzCheck the ticket program플레이그라운드 열기 →
    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))
    기록된 출력종료 코드 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 · 기록에 사용한 토파즈 5.21.3
    기대 결과

    기대 결과

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

    관측한 단언 · 통과 · 5.21.3

    종료 코드 0.

    관측한 표준 출력
    1. data-and-patterns.tpz: types-ok (2 modules)
    2. 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

    링크
    data-and-patterns.tpzPrint both ticket states플레이그라운드 열기 →
    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))
    기록된 출력종료 코드 0
    Docs: open
    Tests: closed
    
    $ topaz run data-and-patterns.tpz --compiler self · 기록에 사용한 토파즈 5.21.3
    기대 결과

    기대 결과

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

    기대 표준 출력
    1. Docs: open
    2. Tests: closed
    관측한 단언 · 통과 · 5.21.3

    종료 코드 0.

    관측한 표준 출력
    1. Docs: open
    2. Tests: 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

    링크

    Functions and generics →

    check →

    std.io.writeLine →

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

    메뉴

    다운로드 →