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

    Generic functions and protocols

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

    Prerequisite

    Link

    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

    Link

    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

    Link

    Check the constrained function

    Link
    describe.tpzCheck the constrained functionOpen in Playground
    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 }))
    Recorded outputexit 0
    describe.tpz: types-ok (2 modules)
    describe.tpz: resolve-ok (2 modules)
    
    $ topaz check describe.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. describe.tpz: types-ok (2 modules)
    2. describe.tpz: resolve-ok (2 modules)

    No stderr.

    • Passed · expected 0; observed 0
    Source evidence

    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

    Link

    Run the function with two record types

    Link
    describe.tpzRun the function with two record typesOpen in Playground
    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 }))
    Recorded outputexit 0
    Label { text: Topaz }
    Count { value: 3 }
    
    $ topaz run describe.tpz --compiler self · recorded with topaz 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. Label { text: Topaz }
    2. Count { value: 3 }
    Observed assertions · passed · 5.21.3

    Exit code 0.

    Observed stdout
    1. Label { text: Topaz }
    2. Count { value: 3 }

    No stderr.

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

    Both calls use the same function body.

    4. Remove a repeated bound

    Link

    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

    Link
    example.tpzRepeated-bound exampleOpen in Playground
    1. function duplicate<T: Show + Show>(value: T) -> T {
    2. value
    3. }
    Expected outputNonzero exit
    $ topaz check example.tpz · target product 5.21.3
    Expected result

    Expected result

    Rejection during check. Nonzero exit.

    • TPZ5523 repeated-protocol-bound at example.tpz:1:30

    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.

    Source evidence

    Verification record

    Link

    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

    Link

    Functions and generics

    TPZ5523: Duplicate protocol bound

    std.io.writeLine

    Menu

    Download