A Tour of Whisker
Every example on this page is a whole program. Paste one into the Playground to run it; the one under Imports uses a Go package, so run it with the whisker command instead.
Bindings
let binds a name to a value. A type annotation is optional: without one, the type comes from the value.
let name = "Hoge"
let age int = 3
print(name, "is", age)A binding cannot be rebound. To get a new value, bind a new name.
Functions
A named function needs a type for each parameter, and a result type when it returns a value. return hands the result back.
fn clamp(n int, lo int, hi int) int {
if n < lo {
return lo
}
if n > hi {
return hi
}
return n
}
print(clamp(15, 0, 10))When the body is a single expression, return can be left out: the expression is the result. It has to be one expression, not one line, so a pipe that spans lines counts too.
fn add(a int, b int) int { a + b }
fn area(w, h float) float { w * h }
fn total(xs list) int {
xs
|> filter(fn(x) { x > 0 })
|> reduce(0, fn(acc, x) { acc + x })
}
print(add(2, 3))
print(area(2.0, 4.5))
print(total([3, -1, 4]))A function without a result type returns nothing, even if its body is one expression.
A lambda is fn without a name. Its body is an expression, and its parameter types may be left out.
let double = fn(x) { x * 2 }
print(double(21))Calling a function with fewer arguments than it takes gives back a function that waits for the rest:
let add = fn(a int, b int) { a + b }
let add10 = add(10)
print(add10(5))Control flow
if and else take no parentheses around the condition.
let n = 7
if n % 2 == 0 {
print("even")
} else if n > 5 {
print("odd and large")
} else {
print("odd")
}for is the only loop. It counts, walks a range, a list or a dict, or repeats while a condition holds.
for i in 3 { print(i) } // 0, 1, 2
for i in 1..3 { print(i) } // 1, 2, 3
for c in ["a", "b"] { print(c) } // each element
for i, c in ["a", "b"] { print(i, c) }
for {
print("once")
break
}match picks the first arm whose pattern fits. Patterns can be values, ranges, guards and _.
fn size(n int) string {
return match n {
0 => "none",
1..9 => "a few",
_ if n < 0 => "less than none",
_ => "lots"
}
}
print(size(0), size(4), size(-1), size(100))Lists and the pipe
map, filter and reduce take a list first, so the pipe |> reads left to right: x |> f(y) is f(x, y).
let words = ["hoge", "foo", "piyo", "fugafuga"]
let long = words
|> filter(fn(w) { len(w) > 3 })
|> map(fn(w) { upper(w) })
print(long)
print(join(long, ", "))Types
A struct groups named fields. Calling the type’s name makes a value.
type User struct {
name string
age int
}
let u = User("Piyo", 5)
print(u.name, u.age)Methods are functions with a receiver. An interface lists methods; a type satisfies it by having them.
type Showable interface {
show() string
}
type User struct { name string }
fn (u User) show() string { "user " + u.name }
print(User("Hoge").show())A struct whose body lists variants is a sum type: a value is one of them. match takes it apart.
type Shape struct {
Circle(radius float)
Rect(w float, h float)
}
fn area(s Shape) float {
return match s {
Circle(r) => 3.0 * r * r,
Rect(w, h) => w * h
}
}
print(area(Circle(1.0)), area(Rect(2.0, 3.0)))type Name = T is an alias, the same type as T. type Name T is a new type, distinct from T, whose underlying value is .value.
type UserId int
let id = UserId(42)
print(id.value)Sized integers
int is 64 bits. Whisker also has every size Go has: int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, with byte for uint8, rune for int32, and float32.
They behave as in Go. Arithmetic wraps around, and converting to a smaller type keeps the low bits:
let b uint8 = 250
print(b + 10) // 4
let big = 1000
print(uint8(big)) // 232
print(int16(big) * 40) // -25536
Two different integer types never meet in one operation; convert one of them. A literal that does not fit its type is a compile error.
let small uint8 = 200
let wide uint32 = 70000
print(uint32(small) + wide)Errors
fail raises an error. recover(x, fallback) catches one: it is x, or fallback when x fails. The fallback is a value, or a function given the error. At the end of a pipe, write x |> recover(fallback).
fn divide(a int, b int) int {
if b == 0 {
fail("division by zero")
}
return a / b
}
print(recover(divide(9, 3), 0))
print(recover(divide(9, 0), 0))
print(recover(divide(9, 0), fn(err error) { -1 }))
print(divide(9, 0) |> recover(-1))try turns a failure into a value, and is_error tells you which you got:
fn half(n int) int {
if n % 2 != 0 {
fail("odd number")
}
return n / 2
}
let r = try(fn() { half(7) })
if is_error(r) {
print("caught:", r)
}Contracts
A named function may say what it needs (requires) and what it promises (ensures) between its signature and its body. Inside an ensures, result is the value the function returns. Every clause is checked each time the function runs, and a broken one is an error that recover catches.
pure fn clamp(x int, lo int, hi int) int
requires lo <= hi
ensures result >= lo
ensures result <= hi
{
if x < lo { return lo }
if x > hi { return hi }
return x
}
print(clamp(15, 0, 10))
print(recover(clamp(1, 5, 2), "caught: lo is above hi"))A contract is also something the tools can use. whisker test tries the contract of each pure fn in the file under test on generated inputs, with no test to write. whisker verify asks the z3 solver whether any input satisfies every requires and still breaks an ensures, and says proved, counterexample, unknown or skipped for each function; it reads integers and bools, and z3 has to be installed. whisker export lean writes the pure functions as Lean 4 definitions and each ensures as a theorem to prove there.
A function named prop_… in a test file is a property: whisker test runs it on generated inputs and shrinks a failing one to a small input. The Quick Reference has the details.
Imports
import go reaches any Go package, by its import path. Whisker has no packages of its own: reading a file, making an HTTP request or telling the time is done with the Go package for it (os, net/http, time). Programs that import a Go package need the whisker command; the Playground runs only the language itself.
import go "strings"
import go "net/url"
print(strings.ToUpper("whisker"))
let u = url.Parse("https://whisker.oreha.dev/learn/tour/")
print(u.host)What a Go function returns is read as a Whisker value. A record becomes a basket, and a field is read with a dot or by key: u.host and u["host"] are the same. A value with methods and nothing to read, such as a client or a connection, is held whole and the next call is made on it.
The Standard Library lists every builtin, and the Specification says exactly what each construct means.