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

    Modules and the standard library

    Move a greeting into another source file, import it, and print through the standard library.

    Prerequisite

    Link

    Use Topaz 5.21.3 with SELF and language mode 5.21. Work in one directory containing the two files in this chapter.

    1. Split the program

    Link

    Save both files with the shown names. greeting.tpz exports greet; main.tpz selects it and imports std.io as io. Keep the output call in main.tpz.

    2. Check the entry file

    Link

    Check both source files

    Link
    main.tpzCheck both source filesOpen in Playground
    1. import greeting { greet }
    2. import std.io
    3. io.writeLine(greet("Topaz"))
    Recorded outputexit 0
    main.tpz: types-ok (3 modules)
    main.tpz: resolve-ok (3 modules)
    
    $ topaz check main.tpz --compiler self · recorded with topaz 5.21.3
    greeting.tpzCheck both source filesOpen in Playground
    1. export function greet(name: string) -> string {
    2. return "Hello, {name}!"
    3. }
    Expected result

    Expected result

    Checking accepts the program. Exit code 0.

    Observed assertions · passed · 5.21.3

    Exit code 0.

    Observed stdout
    1. main.tpz: types-ok (3 modules)
    2. main.tpz: resolve-ok (3 modules)

    No stderr.

    • Passed · expected 0; observed 0
    Source evidence

    Run the command from the directory containing both files. Checking should succeed. If module resolution fails, check the file name and location, then follow the reported diagnostic.

    3. Run the program

    Link

    Run the entry file

    Link
    main.tpzRun the entry fileOpen in Playground
    1. import greeting { greet }
    2. import std.io
    3. io.writeLine(greet("Topaz"))
    Recorded outputexit 0
    Hello, Topaz!
    
    $ topaz run main.tpz --compiler self · recorded with topaz 5.21.3
    greeting.tpzRun the entry fileOpen in Playground
    1. export function greet(name: string) -> string {
    2. return "Hello, {name}!"
    3. }
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. Hello, Topaz!
    Observed assertions · passed · 5.21.3

    Exit code 0.

    Observed stdout
    1. Hello, Topaz!

    No stderr.

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

    The expected program output is Hello, Topaz! followed by a newline. The imported function supplies the greeting; std.io.writeLine writes it.

    Imports and package setup

    Link

    import greeting { greet } selects an exported name; it does not install a dependency. These commands pass a source-file entry. Without an entry operand, package mode loads topaz.toml. See the check reference.

    Verification record

    Link

    Public Topaz 5.21.3 checked and ran this two-file 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.

    Modules and visibility defines imports and exported declarations. Packages, locks and targets describes package inputs.

    Next steps

    Link

    Functions and generics

    std.io.writeLine

    check

    First checked and executed program

    Menu

    Download