A first program
A Weaver source is a .wft file, and a program starts at its main
function. Write this one to hello.wft:
import weaver.io.print
fn main() { print("Hello, world")}wv run compiles it and runs it:
wv run hello.wftwv build leaves a native executable, which depends on nothing but the C
library:
wv build -o hello hello.wftWhile you write, wv check reports the errors without building anything,
and wv build -dev compiles without optimization, with debug information,
about three times faster.
Errors are values
Section titled “Errors are values”A function that can fail says so in its type: T ! E is a T, or the
error E. After a call, ! hands the error to the caller and keeps the
value:
import weaver.io.print
fn double(text: String): Int ! = Int.parse(text)! * 2
fn main() ! { print("${double("21")!}")}An error that main lets out is printed, hello: not an integer, and the
program exits with code 1. Nothing is thrown and nothing unwinds: the
reference has the whole of it.
A test
Section titled “A test”A test is a function marked @test, in the file it tests. Here is
double again, with its test:
import weaver.test.assertEq
fn double(text: String): Int ! = Int.parse(text)! * 2
@test("doubles a number")fn doubles() ! { assertEq(double("21")!, 42)}wv test hello.wftwv build leaves the tests out of the program.