File
File module: filesystem I/O. All I/O functions except exists return Result(value, FileError). Open a File.Handle with open / open_write (or with_open / with_open_write, which close it for you) to read, write and seek a stream; a use after close returns Err(Closed). Use with_lines / with_chunks for streaming. The file handle is closed when the callback returns and if the callback panics — these wrappers delegate to the runtime try_finally primitive to guarantee cleanup even on unhandled exceptions. each_line / each_chunk close the handle on normal return; if f panics they do not close it there, and the descriptor is released only when the handle's last reference is dropped.
Types
The runtime builds these by tag (runtime/march_runtime.c FILEERR_*): keep the order, and add new constructors at the end.
How open_write treats an existing file: Truncate empties it, Append writes after its end. Either way a missing file is created.
FileStat uses a positional constructor Pattern match: FileStat(size, kind, modified, accessed)
Functions
Append data to the end of path, creating the file if it does not exist. Returns Ok(:ok) or Err(FileError).
Examples
File.append("app.log", "started\n")Close h. Returns Ok(()), or Err(Closed) if it was already closed. Every later use of h returns Err(Closed). A read or write still in progress on another task finishes first; the descriptor is released when it does.
Copy the contents of src to dest, creating or overwriting dest. Returns Ok(:ok) or Err(FileError) (reported as IoError(msg)).
Delete the file at path. Returns Ok(:ok), or Err(FileError) if it does not exist or cannot be removed. Use Dir.rmdir / Dir.rm_rf for directories.
Call f(chunk) for each chunk of at most size bytes of path, for side effects. Returns Ok(:ok) or Err(FileError) if the file cannot be opened. If f panics the file is not closed at the panic site: its descriptor is released when the handle's last reference is dropped (use with_chunks to close it at once).
Call f(line) for each line of path (terminators stripped), for side effects. Returns Ok(:ok), or Err(FileError) if the file cannot be opened. Unlike with_lines, the file is not closed at the panic site if f panics: its descriptor is released when the handle's last reference is dropped.
Examples
File.each_line("names.txt", fn name -> println("hello " ++ name))True if any filesystem entry exists at path, including directories. Use Dir.exists to test specifically for directories. Never fails.
Examples
if File.exists("out.log") do File.delete("out.log") else Ok(:ok) endThe modification time of path as a Unix timestamp in seconds (Ok(mtime)), or the FileError from stat.
Open path for reading. Returns Ok(handle), or Err(FileError) such as NotFound(path) or Permission(path). Read it with read_line / read_chunk, and close it when done; a handle that is dropped without being closed is closed when its last reference goes. with_open closes it for you.
Examples
match File.open("notes.txt") do
Ok(h) ->
let first = File.read_line(h)
let _ = File.close(h)
first
Err(e) -> Err(e)
endOpen path for writing, creating it if missing. Truncate empties an existing file first; Append writes after its end. Returns Ok(handle) or Err(FileError). Write with write_to and close the handle when done (the bytes are on disk once it is closed).
Examples
match File.open_write("out.log", File.Append) do
Ok(h) ->
let _ = File.write_to(h, "started\n")
File.close(h)
Err(e) -> Err(e)
endRead the whole file at path into a String. Returns Ok(contents) or Err(FileError), e.g. NotFound(path), Permission(path) or IsDirectory(path).
Examples
match File.read("config.toml") do
Ok(text) -> parse(text)
Err(NotFound(p)) -> defaults()
Err(e) -> panic("cannot read config")
endRead up to size bytes from h. Returns Ok(Some(chunk)) (the last chunk may be shorter), Ok(None) at end of file, or Err(Closed).
Read the next line from h, without its line terminator. Returns Ok(Some(line)), Ok(None) at end of file, or Err(Closed) once h is closed.
Examples
File.read_line(h) -- Ok(Some("first line"))Read the file at path and split it on \n. Returns Ok(lines) without the newline characters; ALL trailing empty lines are dropped (so a final newline, or several, adds no empty element, and an empty file gives Ok([])). A \r before each \n is kept. Errors as for read.
Examples
-- file contents "a\nb\n\n"
File.read_lines("notes.txt") -- Ok(["a", "b"])Rename (move) src to dest, replacing dest if it exists. Returns Ok(:ok) or Err(IoError("<src>: <reason>")).
Move h to the absolute byte offset. Returns Ok(offset) or Err(Closed) / Err(IoError(msg)).
Examples
File.seek(h, 0) -- back to the start: Ok(0)Metadata for path: Ok(FileStat(size_bytes, kind, modified, accessed)) where kind is a FileKind and the times are Unix timestamps in whole seconds. Returns Err(NotFound(path)) if nothing exists there.
Examples
match File.stat("data.bin") do
Ok(FileStat(size, RegularFile, _, _)) -> size
_ -> 0
endLike with_lines, but the Seq yields raw chunks of at most size bytes (the last one may be shorter). Returns Ok(callback_result) or Err(FileError). The file is closed even if callback panics.
Examples
File.with_chunks("blob.bin", 65536, fn chunks ->
Seq.fold(chunks, 0, fn (n, c) -> n + string_byte_length(c)))Open path, pass the callback a single-pass Seq(String) of its lines (line terminators stripped), then close the file. Returns Ok(callback_result), or Err(FileError) if the file cannot be opened. Lines are read lazily as the Seq is folded, so memory stays bounded. The Seq is single-pass: consuming it twice yields nothing the second time, and it must not escape the callback (the file is closed after). try_finally guarantees the fd is closed even if callback panics.
Examples
File.with_lines("big.log", fn lines ->
Seq.fold(lines, 0, fn (n, line) ->
if String.contains(line, "ERROR") do n + 1 else n end))Open path for reading, pass the handle to f, and close it when f returns or panics. Returns Ok(f_result) or the open's Err(FileError). The handle must not be used after f returns: it is closed.
Examples
File.with_open("notes.txt", fn h -> File.read_line(h))Like with_open, for writing: opens path with open_write(path, mode), passes the handle to f, and closes it when f returns or panics.
Examples
File.with_open_write("out.txt", File.Truncate, fn h ->
File.write_to(h, "hello\n"))Write data to path, creating the file or truncating an existing one. Returns Ok(:ok) or Err(FileError) (e.g. the parent directory does not exist).
Examples
File.write("out.txt", "hello\n")