Records, enums and optionals
Records
Section titled “Records”A record groups named fields. Its literal is written with the brace glued
to the type’s name, Point{…}; a field whose value is a variable of the
same name is written once.
import weaver.io.print
type Point = { x: Int, y: Int }
type Config = { name: String, retries: Int = 3 }
fn main() { val x = 4 val p = Point{x, y: 2} val q = p.with(y: 10) print("${p.x}, ${p.y} then ${q.x}, ${q.y}")
val c = Config{name: "fetch"} print("${c.name} retries ${c.retries} times")}4, 2 then 4, 10fetch retries 3 timesRecords are values: p.with(y: 10) makes a modified copy, and p does
not change. A field can have a default value.
Methods
Section titled “Methods”impl gives a type its functions. Those whose first parameter is self
are methods; the others are called on the type:
import weaver.io.print
type Rect = { w: Int, h: Int }
impl Rect { fn square(side: Int): Rect = Rect{w: side, h: side}
fn area(self): Int = self.w * self.h}
fn main() { val r = Rect.square(3) print("${r.area()}")}9An enum is a value that is one of several variants, each of which can
carry fields. match tells them apart, and must cover every variant: the
compiler names the one you forgot.
import weaver.io.print
enum Shape = | Circle(r: Float) | Rect(w: Float, h: Float) | Dot
fn area(s: Shape): Float = match s { Circle(r) => 3.14159 * r * r Rect(w, h) => w * h Dot => 0.0}
fn main() { val shapes = [Circle(r: 1.0), Rect(w: 2.0, h: 3.0), Dot] for s in shapes { print("${s.area()}") }}3.141596.00.0A variant is built like a function call, Circle(r: 1.0). Where the type
is known, .Dot names its variant without repeating the enum.
Optionals
Section titled “Optionals”There is no null. A value that may be absent has the type T?, which is
Some(value) or None, and the compiler makes you handle both:
import weaver.io.print
type User = { name: String, nickname: String? }
fn greeting(u: User): String = "Hello, ${u.nickname ?? u.name}"
fn initial(u: User): Char? = u.nickname?.chars()?.first()
fn main() { val ada = User{name: "Ada", nickname: None} val grace = User{name: "Grace", nickname: "Amazing Grace"} print(greeting(ada)) print(greeting(grace)) if initial(grace) is Some(c) { print("initial: ${c}") }}Hello, AdaHello, Amazing Graceinitial: Aa ?? bis the value ofa, orbwhenaisNone.x?.f()callsfonly ifxis present, and isNoneotherwise.- A
Tis accepted where aT?is expected:nickname: "Amazing Grace".
Unions of literals
Section titled “Unions of literals”A type can list the values it accepts. It is an enum written with its literals, checked at compile time:
import weaver.io.print
type Order = "asc" | "desc"
fn sorted(xs: Int[], order: Order): Int[] = match order { "asc" => xs.sorted() "desc" => xs.sorted().reversed()}
fn main() { print("${sorted([3, 1, 2], "desc")}")}[3, 2, 1]sorted([3, 1, 2], "up") would not compile.
Tuples
Section titled “Tuples”A tuple groups values without naming a type, and destructures:
import weaver.io.print
fn minMax(xs: Int[]): (Int, Int) { var lo = xs[0] var hi = xs[0] for x in xs { lo = lo.min(x) hi = hi.max(x) } return (lo, hi)}
fn main() { val (lo, hi) = minMax([4, 9, 1, 7]) print("${lo}..${hi}")}1..9