Standard library · Time and I/O
- The language
- Agents and tools
- Model providers
- The Anthropic API
- Talking agents
- Standard library
- Glossary
- Package docs
- Telemetry
- Robots
- Servers
- Deploying
- tin.ad
- The tin command
- How it works
On this page
Time and I/O
Clocks, waiting, input, the environment and HTTP. Each of these reaches outside the program, so tin caps reports it.
Example
let started = now()
sleep(20ms)
print(now() - started >= 20ms)
print(env("TIN_NO_SUCH_VARIABLE").isEmpty()) true
true All 26 symbols
HttpResponseunixMillisnowsleepwithineprintinputenvargsspanspanAttrfetchreadBytesreadFilewriteByteswriteFileexistsisDirlistDirhttpHttpResponse.readHttpResponse.bodyHttpResponse.textHttpResponse.closeassertEqassert
HttpResponse
type HttpResponse = { status: int, headers: Map<string, string> } An HTTP response. The body arrives in chunks: read() them, or take it whole with body()/text().
unixMillis
fn unixMillis(): float Unix time in milliseconds, from the wall clock (for dates and signatures; use now() to measure time).
now
fn now(): Duration The time since the program started.
sleep
fn sleep(time: Duration) Pauses this task for `time`. Other tasks keep running.
within
fn within<T>(t: Task<T>, limit: Duration): Option<T> Waits at most `limit` for a task. Some(result) if it finishes in time; otherwise the task and everything it started are stopped, and the answer is None.
eprint
fn eprint(s: string) Print a line on stderr (diagnostics, progress), not stdout.
input
fn input(prompt: string): string Shows `prompt` and reads one line from the user.
env
fn env(name: string): string The value of an environment variable, or "" if it is not set.
args
fn args(): string[] The command-line arguments: `tin run f.tin a b` gives ["a", "b"]; everything after `--` too. Empty in a browser.
span
fn span<T>(name: string, body: () => T): T Runs \`body\` inside a telemetry span named \`name\` and returns what it returns. Written \`span("load") { ... }\` as well. Agent calls, tool calls and fetch() inside become its children. Without telemetry (no OTEL_* configuration) it just runs \`body\`.
spanAttr
fn spanAttr(key: string, value: string) Sets an attribute on the innermost \`span\` the code runs in (nothing without telemetry). Values are exported as text (a key written \`"#name"\` exports an integer); secrets read with env() are redacted.
fetch
fn fetch(url: string): Result<string, string> The body of an HTTP GET as text, or an Err with the reason it failed.
readBytes
fn readBytes(path: string): Result<Bytes, string> A file's bytes. Needs \`fs.read\`. Err says why (no such file, a directory, no permission...).
readFile
fn readFile(path: string): Result<string, string> A file's text (it must be UTF-8). Needs \`fs.read\`.
writeBytes
fn writeBytes(path: string, data: Bytes): Result<int, string> Write bytes to a file, replacing it (its directory must exist). Ok is the number of bytes. Needs \`fs.write\`.
writeFile
fn writeFile(path: string, text: string): Result<int, string> Write text to a file as UTF-8, replacing it. Ok is the number of bytes. Needs \`fs.write\`.
exists
fn exists(path: string): bool Is there a file or directory at path? Needs \`fs.read\`.
isDir
fn isDir(path: string): bool Is path a directory? Needs \`fs.read\`.
listDir
fn listDir(path: string): Result<string[], string> The names in a directory (no \`.\` or \`..\`), sorted. Needs \`fs.read\`.
http
fn http(method: string, url: string, headers: ()<string, string>[], body: Bytes): Result<HttpResponse, string> Send an HTTP request and wait for the status line and headers (not the body). headers are (name, value) pairs; header names in the response are lower-case. Needs \`net\`. Err only when there is no response (DNS, refused, TLS...): a 404 or 500 is an Ok with that status.
HttpResponse.read
fn HttpResponse.read(r: HttpResponse): Result<Option<Bytes>, string> The next chunk of the body, None at the end (the response is then closed).
HttpResponse.body
fn HttpResponse.body(r: HttpResponse): Result<Bytes, string> The rest of the body, all of it.
HttpResponse.text
fn HttpResponse.text(r: HttpResponse): Result<string, string> The rest of the body as text (it must be UTF-8).
HttpResponse.close
fn HttpResponse.close(r: HttpResponse) Stop reading: drops the connection. Safe to call more than once.
assertEq
fn assertEq<T>(actual: T, expected: T) Panics unless the two values print the same, showing both. Works on every type; made for tests.
assert
fn assert(condition: bool, message: string) Panics with `message` when `condition` is false.