Skip to content

File System & I/O

File and stream I/O in Wysna is designed around deterministic resource cleanup and explicit error handling with fallible channels (T ! E).

Reading and Writing Files (Prelude Built-ins)

Section titled “Reading and Writing Files (Prelude Built-ins)”

Wysna includes first-class built-in functions in the prelude for common file operations without requiring module imports:

fn main() -> unit {
let result = read_file("config.json") else err {
print("Failed to read file:", err.message)
return
}
print("File Content:")
print(result)
}

Because reading can fail (e.g., file not found or permission denied), read_file returns str ! IoError.

To write UTF-8 text to a file on disk:

fn save_report(filename: str, content: str) -> unit ! IoError {
write_file(filename, content)?
print("Report saved successfully to:", filename)
}
fn main() -> unit {
save_report("output.txt", "Wysna v0.5.0 compilation report\n") else err {
print("Error saving report:", err.message)
}
}

For low-level filesystem manipulations (checking existence, creating directories, deleting or renaming files), import "std/fs.wysna":

import "std/fs.wysna"
fn main() -> unit {
# Check if a path exists
let is_present = fs.exists("data/cache") else err {
false
}
if is_present {
print("Cache directory exists")
} else {
fs.mkdir("data/cache") else err {
print("Failed to create directory:", err.operation)
}
}
}

Available std/fs functions:

  • fs.exists(path: str) -> bool ! FsError
  • fs.size(path: str) -> int ! FsError
  • fs.mkdir(path: str) -> unit ! FsError
  • fs.remove(path: str) -> unit ! FsError
  • fs.rename(path: str, target: str) -> unit ! FsError