Find a standard function for the task you want to finish, from converting text to reading a file. The compiler checks names, members, and argument types before execution.
Install Topaz to run the examples.
Choose a capability
| Task | Start here | Result to handle |
|---|---|---|
| Print text, parse a number, or inspect Unicode | Core Library | A value or Option |
| Transform arrays, find a key, or remove duplicates | Collection Methods | A collection, bool, or Option |
| Assemble bytes, calculate a checksum, or compress data | Core Library | Bytes, a number, or Result |
| Read or write a whole file | Files & Resources | Result from std.fs |
| Keep a file open for several operations | Files & Resources | Result from io.open, then cleanup with using or defer |
| Understand numeric output, collection order, or an execution error | Runtime Behavior | A value or an error that stops execution |
An everyday recipe
Save this as overview.tpz.
import std.io
let parsed: Option<int> = "42".toInt()
let values: Array<int> = [1, 2, 3]
let doubled = values.map(value => value * 2)
io.writeLine("{parsed}:{doubled}")Run topaz run --language-version 5.21 overview.tpz in the folder containing that file.
Some(42):[2, 4, 6]"42".toInt() returns Some(42). Changing the input to "x" gives None. Both are Option values, so the caller can handle a failed conversion. map doubles each array element and keeps the order.
Use Result when an operation returns an error reason, such as a failed file read. A runtime error, or fault, stops execution. Invalid names and argument types are checking errors. See Null, Option, Result & Faults for handling these cases.
Find names and imports
Receiver methods such as string.toInt() and the Some, None, Ok, and Err constructors need no import. Import std.io for io.writeLine and io.readStdin.
Construct values through their types, then use receiver methods. Parsing, serialization, file access, and test checks live in the corresponding std.* modules.
| Task | Canonical API |
|---|---|
| Store ordered elements | Array |
| Find values by key | Map |
| Keep unique elements | Set |
| Read or write data formats | std.json, std.csv, std.toml, URL |
| Store or assemble bytes | Bytes, ByteBuffer |
| Encode or compress data | string.encodeUtf8, Bytes methods |
| Calculate hashes or checksums | Bytes methods |
| Match text patterns | Regex, Match |
| Calculate with numbers | BigInt, Decimal |
| Work with calendar dates | Date |
| Access files or construct paths | std.fs, Path |
| Parse command-line arguments | Array<string> methods |
| Break paragraphs into lines for typesetting | std.typeset |
| Check assertions in the test profile | std.test.assert |
Package code can also import the toolchain's std.* modules, including helpers for generators and parsers. An imported module with the same name hides the built-in namespace in that file. The self compiler and Rust reference compiler apply the same name lookup rules.
Exact boundaries
A profile specifies which operations a program may use. File and other host operations also need support from the environment running Topaz. Importing a module does not grant filesystem, network, process, environment, clock, or database permissions.
- String positions count Unicode scalar values. A scalar is one Unicode code point outside the surrogate range. Byte APIs state when they count bytes.
- Collections keep their specified iteration and snapshot order.
- Parsing and decoding return the declared
OptionorResult. - Invalid arithmetic and invalid direct array access can stop execution with a fault.
- The test profile accepts
std.test.assert. It rejects otherstd.testframework operations. A check without--profiletreats them as ordinary imports. - Unknown names, modules, and members are rejected during checking. Check the spelling and the API for that type.
Continue by task
- Core Library for numbers, text, and bytes.
- Collection Methods for lookup and mutation.
- Files & Resources for reading, writing, and closing files.
- Runtime Behavior for output and execution errors.