Values and functions
Bindings
Section titled “Bindings”val binds a name to a value once; var binds one you can reassign.
The type is inferred, and can be written after a colon:
import weaver.io.print
fn main() { val name = "Ada" val year: Int = 1815 var age = 0 age = 2026 - year print("${name} would be ${age}")}Ada would be 211"${…}" interpolates any expression into a string. print comes from
the standard library, and each file imports what it uses.
Numbers
Section titled “Numbers”Int is a 64-bit integer, Float a 64-bit floating-point number; there
are sized ones too, Int32, UInt8 and so on. Nothing converts between
them on its own: you write the conversion.
import weaver.io.print
fn main() { val count = 7 val total = 2.5 val mean = total / count.toFloat() print("mean: ${mean.format(precision: 3)}") print("7 / 2 = ${7 / 2}, 7 % 2 = ${7 % 2}")}mean: 0.3577 / 2 = 3, 7 % 2 = 1Arithmetic is checked: an integer overflow or a division by zero stops the program rather than give a wrong result.
Functions
Section titled “Functions”A function whose body is one expression writes it after =. A body in
braces returns its value with return: its last expression is not
returned implicitly, so that a reader always sees where a function ends.
import weaver.io.print
fn square(x: Int): Int = x * x
fn hypotenuse(a: Float, b: Float): Float { val sum = a * a + b * b return sum.sqrt()}
fn main() { print("${square(12)}") print("${hypotenuse(3.0, 4.0)}")}1445.0The return type follows the parameters, after :. A function that returns
nothing writes no type.
Calling a function
Section titled “Calling a function”Any function can be called like a method on its first argument: x.f(y)
is f(x, y). Parameters can be named at the call, and can have default
values:
import weaver.io.print
fn greet(name: String, greeting: String = "Hello"): String = "${greeting}, ${name}!"
fn shout(text: String): String = text.toUpper()
fn main() { print(greet("Ada")) print(greet("Grace", greeting: "Welcome")) print("Linus".greet().shout()) print("Ken" |> greet() |> shout())}Hello, Ada!Welcome, Grace!HELLO, LINUS!HELLO, KEN!a |> f(b) is another way to write a.f(b), for long chains. There is
no overloading by type: two functions differ by their names, or by the
names of their parameters.
Strings
Section titled “Strings”A String is UTF-8 text. A string in triple quotes spans several lines,
its indentation taken from the closing quotes:
import weaver.io.print
fn main() { val word = "naïve" print("${word.byteLen()} bytes, ${word.chars().len()} characters") for ch in word { if !ch.isAsciiAlphabetic() { print("not ASCII: ${ch}") } } print(""" Usage: greet <name>
Says hello. """)}6 bytes, 5 charactersnot ASCII: ïUsage: greet <name>
Says hello.A test is a function marked @test, beside the code it tests, and wv test main.wft runs it:
import weaver.test.assertEq
fn square(x: Int): Int = x * x
@test("squares a negative number")fn squaresNegative() { assertEq(square(-3), 9)}wv build and wv run leave the tests out.