Reference

Standard Library Overview

Find standard functions for text, numbers, collections, bytes, and files, then choose how to handle missing values and errors.

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

TaskStart hereResult to handle
Print text, parse a number, or inspect UnicodeCore LibraryA value or Option
Transform arrays, find a key, or remove duplicatesCollection MethodsA collection, bool, or Option
Assemble bytes, calculate a checksum, or compress dataCore LibraryBytes, a number, or Result
Read or write a whole fileFiles & ResourcesResult from std.fs
Keep a file open for several operationsFiles & ResourcesResult from io.open, then cleanup with using or defer
Understand numeric output, collection order, or an execution errorRuntime BehaviorA value or an error that stops execution

An everyday recipe

Save this as overview.tpz.

TOPAZ
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.

Output
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.

TaskCanonical API
Store ordered elementsArray
Find values by keyMap
Keep unique elementsSet
Read or write data formatsstd.json, std.csv, std.toml, URL
Store or assemble bytesBytes, ByteBuffer
Encode or compress datastring.encodeUtf8, Bytes methods
Calculate hashes or checksumsBytes methods
Match text patternsRegex, Match
Calculate with numbersBigInt, Decimal
Work with calendar datesDate
Access files or construct pathsstd.fs, Path
Parse command-line argumentsArray<string> methods
Break paragraphs into lines for typesettingstd.typeset
Check assertions in the test profilestd.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 Option or Result.
  • Invalid arithmetic and invalid direct array access can stop execution with a fault.
  • The test profile accepts std.test.assert. It rejects other std.test framework operations. A check without --profile treats 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