write(s: string) - > Result<(), string>- Module
- std.
io - Receiver
- File
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.io in language mode 5.21.
The current compiler selects std/io.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1017 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 67 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1017 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 390 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296
Receiver
File
Parameter constraints
open evaluates its path once and opens an existing regular file for reading and writing. It does not create or truncate a file. A successful result is Ok containing a File with the cursor at the first byte.
String paths use the host path syntax. Relative paths resolve against the working directory for an ordinary installed CLI invocation. A package host applies its configured filesystem base and capability policy. NUL, an empty path, a missing file, a directory, and denied read or write access return Err containing an error string.
An open error follows the filesystem error context contract with open as the public operation. An error result does not grant a handle, and path validation does not bypass operating system or package permissions.
File values refer to a resource identity. Copies share the open state and cursor. The handle continues to identify the opened file when the directory entry is renamed or replaced. This is the host resource exception in collection value semantics.
read consumes the remaining bytes from the shared cursor through EOF. UTF-8 validity and text preservation follow UTF-8 decoding. A successful read returns Ok containing text, and a successful read at EOF returns an empty string.
A read error returns Err containing an error string without a text value. Bytes already consumed before a host or decoding error are not restored. Callers must not assume the previous cursor position after a read error.
write encodes its string as UTF-8 and writes all bytes at the shared cursor. It adds no newline. Success advances the cursor by the encoded byte count, overwrites those bytes, and preserves an existing suffix beyond them. A write at EOF extends the file.
An empty write succeeds without moving the cursor. A closed handle, denied access, or host write failure returns Err. A host failure may leave a written prefix and an advanced cursor. It does not roll back earlier argument effects or file changes.
close returns unit, releases the resource, and marks every alias closed. Closing an already closed resource also returns unit. A later read or write through any alias returns Err. A host failure while closing produces a structured runtime fault at the close call.
The Topaz 6.0 host close fault is TPZ4009. Its primary location is the close call. A successful write or close does not promise persistence across a system crash.
Receiver and argument expressions are evaluated once in source order before each resource operation. Use std.fs for whole file replacement or whole file reads. Use File when successive operations must share a cursor and lifetime.
Related rule: Use a file handle with a shared cursorResult
The declared result type is Result<(), string>.
Effects
Failures
Result and effects
LinkWrite UTF-8 bytes at the shared cursor without adding a newline or removing a later suffix. Return a recoverable error on failure.
Related entries
LinkUse a file handle with a shared cursor
LinkUse a file handle with a shared cursor
open evaluates its path once and opens an existing regular file for reading and writing. It does not create or truncate a file. A successful result is Ok containing a File with the cursor at the first byte.
String paths use the host path syntax. Relative paths resolve against the working directory for an ordinary installed CLI invocation. A package host applies its configured filesystem base and capability policy. NUL, an empty path, a missing file, a directory, and denied read or write access return Err containing an error string.
An open error follows the filesystem error context contract with open as the public operation. An error result does not grant a handle, and path validation does not bypass operating system or package permissions.
File values refer to a resource identity. Copies share the open state and cursor. The handle continues to identify the opened file when the directory entry is renamed or replaced. This is the host resource exception in collection value semantics.
read consumes the remaining bytes from the shared cursor through EOF. UTF-8 validity and text preservation follow UTF-8 decoding. A successful read returns Ok containing text, and a successful read at EOF returns an empty string.
A read error returns Err containing an error string without a text value. Bytes already consumed before a host or decoding error are not restored. Callers must not assume the previous cursor position after a read error.
write encodes its string as UTF-8 and writes all bytes at the shared cursor. It adds no newline. Success advances the cursor by the encoded byte count, overwrites those bytes, and preserves an existing suffix beyond them. A write at EOF extends the file.
An empty write succeeds without moving the cursor. A closed handle, denied access, or host write failure returns Err. A host failure may leave a written prefix and an advanced cursor. It does not roll back earlier argument effects or file changes.
close returns unit, releases the resource, and marks every alias closed. Closing an already closed resource also returns unit. A later read or write through any alias returns Err. A host failure while closing produces a structured runtime fault at the close call.
The Topaz 6.0 host close fault is TPZ4009. Its primary location is the close call. A successful write or close does not promise persistence across a system crash.
Receiver and argument expressions are evaluated once in source order before each resource operation. Use std.fs for whole file replacement or whole file reads. Use File when successive operations must share a cursor and lifetime.
Why
A handle gives successive operations one cursor and one lifetime. Whole file operations already belong to std.fs, so rereading and replacing by path would duplicate their meaning. An existing file open avoids surprising creation or truncation, and recoverable access failures stay in Result.
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Open and close an existing file without changing its bytes
Linkimport std.ioimport std.fsimport std.testlet file = test.assertOk(io.open("source.txt"))file.close()io.writeLine(test.assertOk(fs.readText("source.txt")))let missing = test.assertErr(io.open("missing.txt"))let named = missing.contains("missing.txt")io.writeLine("missing {named}")
original missing true
original
Expected result
Expected result
Execution completes. Exit code 0.
originalmissing true
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Reject a directory when opening a file
Linkimport std.iomatch (io.open(".")) {case Err(_) => io.writeLine("directory rejected")case Ok(file) => {file.close()io.writeLine("directory accepted")}}
directory rejected
Expected result
Expected result
Execution completes. Exit code 0.
directory rejected
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Share the cursor across file aliases
Linkimport std.ioimport std.testlet file = test.assertOk(io.open("source.txt"))let alias = fileio.writeLine(test.assertOk(file.read()))let tail = test.assertOk(alias.read())let atEnd = tail == ""io.writeLine("{atEnd}")alias.close()
alpha true
alpha
Expected result
Expected result
Execution completes. Exit code 0.
alphatrue
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Write at the cursor and retain the rest of the file
Linkimport std.ioimport std.fsimport std.testlet file = test.assertOk(io.open("source.txt"))test.assertOk(file.write("XY"))let tail = test.assertOk(file.read())io.writeLine("tail {tail}")file.close()let whole = test.assertOk(fs.readText("source.txt"))io.writeLine("whole {whole}")
tail cdef whole XYcdef
abcdef
Expected result
Expected result
Execution completes. Exit code 0.
tail cdefwhole XYcdef
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Extend a file after reading through EOF
Linkimport std.ioimport std.fsimport std.testlet file = test.assertOk(io.open("source.txt"))test.assertOk(file.read())test.assertOk(file.write("β"))file.close()io.writeLine(test.assertOk(fs.readText("source.txt")))
alphaβ
alpha
Expected result
Expected result
Execution completes. Exit code 0.
alphaβ
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Close every alias and reject later access
Linkimport std.ioimport std.fsimport std.testlet file = test.assertOk(io.open("source.txt"))let alias = filefile.close()alias.close()test.assertErr(alias.read())io.writeLine("read rejected")test.assertErr(alias.write("changed"))io.writeLine("write rejected")io.writeLine(test.assertOk(fs.readText("source.txt")))
read rejected write rejected original
original
Expected result
Expected result
Execution completes. Exit code 0.
read rejectedwrite rejectedoriginal
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi
Return a file read error for invalid UTF-8
Linkimport std.ioimport std.fsimport std.bytes { Bytes }import std.testlet invalid = test.assertOk(Bytes.fromArray([255]))test.assertOk(fs.writeBytes("invalid.txt", invalid))let file = test.assertOk(io.open("invalid.txt"))test.assertErr(file.read())file.close()io.writeLine("invalid text rejected")
invalid text rejected
Expected result
Expected result
Execution completes. Exit code 0.
invalid text rejected
Source evidence
- compiler/crates/topaz_resolve/src/std/521/io.tpz at
912a5e86e1f9· 5 - compiler/crates/topaz_value/src/value/runtime.rs at
912a5e86e1f9· 390 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 98 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 192 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 214 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 226 - compiler/crates/topaz_host_native/src/lib.rs at
912a5e86e1f9· 238 -
topaz.stdlib.contract.file-resourcesatworking-tree - topaz.decision.file-stream-boundaries.1 at
topaz.decisi