Rust safety
with Go compilation times.
Fjord is a small linear language. Memory is safe without lifetimes or a collector, every function shows in its signature what it may touch, and the type checker answers in milliseconds.
// main receives what the program may touch: here, the console.
fn main(console: inout Console) {
console.println("Hello from Fjord")
}I
A program's signature is its permission list
Files, the network, the clock and the console are values handed to main. A function can only use what it is given, so a reviewer learns what code can reach by reading one line.
fn main(console: inout Console, files: FileRead, net: NetListen) {
// may print, read files and accept connections; may not write files,
// open connections of its own or read the clock
}II
Values move
Every value is used exactly once, unless its type allows copying or dropping it. There is no garbage collector and no lifetime annotation, and the checker's soundness is proved in Lean.
type Account = { owner: String, balance: Int } derives drop
fn deposit(account: Account, amount: Int) -> Account {
let Account { owner, balance } = account
Account { owner, balance: balance + amount }
}III
Borrowing without lifetimes
A ref lends a value for the length of a call, an inout lends it to be changed. Loans never outlive the call, so there is nothing to annotate.
fn total(prices: ref Vec[Int]) -> Int {
let sum = 0
for ref price in prices {
sum = sum + price
}
sum
}IV
Errors are values
A failure is a Result like any other value, and ? returns it to the caller. Nothing is thrown, and nothing is silently lost.
type Config = { port: Int } derives drop
fn load(files: ref FileRead) -> Result[Config, String] {
let text = files.read("config.json")?
let port = json::parse(text)?.field("port")?.int()?
Ok(Config { port })
}V
One program, two places
This page is a Fjord program. The server rendered it with view, and the same view and update, compiled to WebAssembly, took it over in your browser.
// The client: decode the server's model, take over its page.
fn main(page: tea::Page) {
let flags = tea::flags(page)
let model = site::decode(flags).unwrap_or(site::init(site::Home))
let (app, ops) = tea::App::hydrate(model, site::update, site::view)
tea::run(app, ops, page)
}