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

문서 검색

명세 6.0

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

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

    투어

    제네릭 함수와 프로토콜

    Reuse one function for two record types, then correct a repeated protocol bound.

    Prerequisite

    링크

    Use Topaz 5.21.3 with the SELF compiler and language mode 5.21. Start with First checked and executed program if you have not run a file yet.

    1. Reuse one constrained function

    링크

    Save the source as describe.tpz. Each record derives Show; T: Show lets describe call Show.show(value). The calls infer different record types. See Type parameters and bounds.

    2. Check the program

    링크

    Check the constrained function

    링크
    describe.tpzCheck the constrained function플레이그라운드 열기 →
    1. import std.io
    2. record Label derives Show {
    3. text: string,
    4. }
    5. record Count derives Show {
    6. value: int,
    7. }
    8. function describe<T: Show>(value: T) -> string {
    9. Show.show(value)
    10. }
    11. io.writeLine(describe(Label { text: "Topaz" }))
    12. io.writeLine(describe(Count { value: 3 }))
    기록된 출력종료 코드 0
    describe.tpz: types-ok (2 modules)
    describe.tpz: resolve-ok (2 modules)
    
    $ topaz check describe.tpz --compiler self · 기록에 사용한 토파즈 5.21.3
    기대 결과

    기대 결과

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

    관측한 단언 · 통과 · 5.21.3

    종료 코드 0.

    관측한 표준 출력
    1. describe.tpz: types-ok (2 modules)
    2. describe.tpz: resolve-ok (2 modules)

    표준 오류 출력 없음.

    • 통과 · expected 0; observed 0

    Expected result: checking succeeds. If it fails, follow the reported code to its diagnostic entry and check the function's bound and argument types.

    3. Run both calls

    링크

    Run the function with two record types

    링크
    describe.tpzRun the function with two record types플레이그라운드 열기 →
    1. import std.io
    2. record Label derives Show {
    3. text: string,
    4. }
    5. record Count derives Show {
    6. value: int,
    7. }
    8. function describe<T: Show>(value: T) -> string {
    9. Show.show(value)
    10. }
    11. io.writeLine(describe(Label { text: "Topaz" }))
    12. io.writeLine(describe(Count { value: 3 }))
    기록된 출력종료 코드 0
    Label { text: Topaz }
    Count { value: 3 }
    
    $ topaz run describe.tpz --compiler self · 기록에 사용한 토파즈 5.21.3
    기대 결과

    기대 결과

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

    기대 표준 출력
    1. Label { text: Topaz }
    2. Count { value: 3 }
    관측한 단언 · 통과 · 5.21.3

    종료 코드 0.

    관측한 표준 출력
    1. Label { text: Topaz }
    2. Count { value: 3 }

    표준 오류 출력 없음.

    • 통과 · expected 0; observed 0
    • 통과 · stdout matches the authored expectation
    • 통과 · stderr matches the authored expectation

    Both calls use the same function body.

    4. Remove a repeated bound

    링크

    Compare the rejected declaration with describe<T: Show>. The second Show repeats a bound for the same type parameter. Remove + Show and check again. See TPZ5523, the Language rule, and the repaired declaration.

    Repeated-bound example

    링크
    example.tpzRepeated-bound example플레이그라운드 열기 →
    1. function duplicate<T: Show + Show>(value: T) -> T {
    2. value
    3. }
    기대 출력0이 아닌 종료 코드
    $ topaz check example.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    TPZ5523 points to the second Show at example.tpz:1:30. Each later occurrence for the same type parameter is diagnosed. Checking continues, so other applicable diagnostics may also appear. Remove the repeated bound to correct this program. The earlier receipt predates the location assertion; CI must execute this revision.

    Observed: The authored exit and diagnostic-cause assertions passed. The primary-location assertion and additional-diagnostics policy remain pending. Recorded with Topaz 5.21.3 in run 42147883818 at 2026-11-06T02:04:56.5052224Z. Document source: 1d6f9265d918.

    Verification record

    링크

    Public Topaz 5.21.3 passed the check and run expectations in Docs run 42224592687, attempt 2. The recorded results were compared with this page's current product assertions using unchanged fixture files, invocations and expectations. The shared TPZ5523 rejection specifies its source location and additional-diagnostic policy. Its current example revision awaits a product run. The derived Show representation contract is listed below.

    Protocols and implementations defines conformance, protocol calls and derived implementations. Functions and generics defines function type parameters and calls.

    Next steps

    링크

    Functions and generics →

    TPZ5523: Duplicate protocol bound →

    std.io.writeLine →

    메뉴

    다운로드 →