Перейти к содержимому
Топаз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. }
    Ожидаемый выводНенулевой код завершения
    $ topaz check example.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения check. Ненулевой код завершения.

    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 →

    Меню

    Загрузка →