Standard Library & Runtime

Core Library

Use general-purpose helpers first, then choose the documented binary and codec interfaces for byte-oriented work.

The core library handles common application tasks: printing text, converting values, inspecting Unicode, representing absence or failure, and transforming collections. Start here before reaching for a specialized namespace.

Choose the everyday helper

TaskUseResult
Print one lineprint("text")()
Parse an integer from texttoInt(text)Option<int>
Parse with a radix from 2 through 36toIntRadix(text, radix)Option<int>
Convert an int to a floattoFloat(value)float
Construct a Unicode scalar from its code pointfromCodePoint(value)Option<string>
Inspect a string by Unicode scalartext.scalars()Array<string>
Transform, select, or fold iterable valuesmap, filter, reduceA new value

Some, None, Ok, and Err are prelude constructors. An unannotated None, Ok, Err, or empty collection requires an expected type when inference cannot determine every type argument.

Parse and transform values

This program retains failed parses as None and counts Korean text by Unicode scalar count rather than byte count.

TOPAZ
function parseAll(values: Array<string>) -> Array<Option<int>> {
    return map(values, value => toInt(value))
}

let scalars = "토파즈".scalars()
let parsed = parseAll(["1", "x", "3"])
print("{scalars.length}:{parsed}")
Output
3:[Some(1), None, Some(3)]

print accepts a string, so interpolation renders the conversion visible. toInt returns None for "x". map does not turn ordinary absence into a fault. Use match when the caller needs to convert absence into a descriptive Result.

Common correction: toFloat converts an int. It does not parse a string. There is no general free toString, len, println, or type_of function. Use interpolation, specific type members such as .length, and documented conversion helpers instead.

Public surface

Use these standard names:

  • print(string) -> () writes one line. input() -> string returns the complete host-provided text payload for that run rather than reading line by line.
  • toInt(string) -> Option<int> and toIntRadix(string, int) -> Option<int> parse integers.
  • toFloat(int) -> float converts an integer. It is not a text parser.
  • fromCodePoint(int) -> Option<string> constructs one valid Unicode scalar.
  • text.scalars() -> Array<string> exposes a string as Unicode scalars.
  • Some and None construct Option. Ok and Err construct Result.
  • option.okOr(error: E) -> Result<T, E> evaluates the error eagerly. option.okOrElse(f: () -> E) -> Result<T, E> calls f only for None.
  • option.map(f: (T) -> U) -> Option<U> transforms Some. option.flatMap(f: (T) -> Option<U>) -> Option<U> avoids a nested Option. Both preserve None without calling f.
  • result.map(f: (T) -> U) -> Result<U, E> transforms Ok. result.flatMap(f: (T) -> Result<U, E>) -> Result<U, E> avoids a nested Result. Both preserve Err without calling f.
  • Top-level map, filter, and reduce transform iterable values. They operate independently from the receiver helpers above, and none of these helpers catches a fault.

The Bytes, ByteBuffer, Hash, and Codec sections below list their public members and exact signatures. Similarly named helpers inside a runtime or generated target remain internal to that implementation.

Assemble bytes in place

Use ByteBuffer.allocate(length, value = 0) when an algorithm must write many bytes into one contiguous fixed-length value. set, fill, and copy require a let mut receiver root. Every range and byte value is checked before the first write, so a fault cannot leave a partially changed target.

TOPAZ
let mut row = ByteBuffer.allocate(8)
row.fill(0, 8, 255)
row.set(0, 137)

let mut frame = ByteBuffer.allocate(12)
frame.copy(row, 0, 2, 8)
let payload = frame.toBytes()
print(payload.toHex())
Output
000089ffffffffffffff0000

Assignment shares a ByteBuffer's mutable identity. ByteBuffer.fromBytes copies into a new buffer, and toBytes() returns an immutable snapshot. A ByteBuffer cannot cross a Web export, participate in equality or ordering, serve as a Map or Set key, enter text or JSON, or be used after an invalid range is silently repaired.

Compute a CRC-32 integrity field

Hash.crc32(data) returns the unsigned CRC-32/ISO-HDLC value. It is useful for file-format integrity fields, not for authentication or adversarial tamper resistance.

TOPAZ
let checksum = Hash.crc32(Bytes.encodeUtf8("123456789"))
print("{checksum}")
Output
3421780262

std.hash.crc32 exposes the same operation through the fixed standard-module surface.

Produce deterministic fixed DEFLATE

Use Codec.deflateFixedCompress(bytes) for one canonical raw fixed-Huffman DEFLATE block without zlib or gzip framing.

TOPAZ
match Codec.deflateFixedCompress(Bytes.encodeUtf8("hello")) {
  case Ok(compressed) => print(compressed.toHex())
  case Err(error) => print(error)
}
Output
cb48cdc9c90700

Input above 256 MiB returns Err, setting the operation's search and memory ceiling. Generated Python does not expose this Codec member and declines before writing an artifact.

Produce deterministic fixed zlib

Use Codec.zlibFixedCompress(bytes) when the format needs a complete zlib stream: the fixed 78 01 header, fixed-Huffman payload, and Adler-32 trailer.

TOPAZ
match Codec.zlibFixedCompress(Bytes.encodeUtf8("hello")) {
  case Ok(compressed) => print(compressed.toHex())
  case Err(error) => print(error)
}
Output
7801cb48cdc9c90700062c0215

Input above 256 MiB returns Err. Generated Python rejects this Codec member before artifact output. This member's API is fixed zlib compression; decompression, levels, dictionaries, streaming, and general Adler-32 operations use their own API surfaces.

Add Reed–Solomon protection bytes

Codec.reedSolomon255223Protect(bytes) emits systematic RS(255,223) codewords. Each 223-byte data shard is followed by 32 parity bytes, and the final data shard is zero-padded.

TOPAZ
match Codec.reedSolomon255223Protect(Bytes.encodeUtf8("hello")) {
  case Ok(protected) => print("{protected.length()}")
  case Err(error) => print(error)
}
Output
255

The caller retains the original logical length. Input must be non-empty and may use at most 65,535 shards. The function creates protection bytes. It does not detect or repair corruption. Generated Python does not expose this Codec member and declines before artifact creation.

Exact boundaries

  • String operations use scalar positions unless their name explicitly specifies bytes.
  • Option.map preserves ordinary absence. Result.map does not catch faults.
  • map, filter, and reduce accept only the fixed iterable families.
  • Typed JSON needs one written, fully known target and returns a $-rooted error path for data failure.
  • input and print require an environment that provides their host capability.
  • assert is a function only in the test profile, never a language keyword.

Continue with Standard Library Overview, Null, Option, Result & Faults, or Runtime Behavior.