Every program on this page was compiled by a script and its output
compared with what is shown here. The compiler's own progress lines, which it prints on
stderr, are left out.
Nil is part of the type
A str? may be nil; a str may not. Reading through a value that might
be nil is a compile error that tells you the three ways out.
def findUser(id: int): str?
if id == 1
return "ada"
return nil
def main()
var name = findUser(2)
print(name.len)
nil_bad.zbr:8:11: error: 'name' may be nil here, so `.len` cannot be read from it -- check it first (if x != nil ...), unwrap it (x!), or give a default (x orelse "")
After a check, the compiler knows:
def findUser(id: int): str?
if id == 1
return "ada"
return nil
def main()
var name = findUser(1)
if name != nil
print("hello, ${name}")
print(findUser(2) orelse "nobody")
hello, ada
nobody
Unions you cannot forget a case of
A branch over a union must handle every variant or say else. Add a
variant later and each branch that misses it stops compiling.
struct Rect
var w: float
var h: float
union Shape
circle: float
rect: Rect
point
def area(s: Shape): float
branch s
on Shape.circle as r return 3.14159 * r * r
on Shape.rect as b return b.w * b.h
on Shape.point return 0.0
def main()
var shapes = [Shape.circle(1.0), Shape.rect(Rect(w: 2.0, h: 3.0)), Shape.point()]
for s in shapes
print(area(s))
3.14159
6.0
0.0
Errors you can see at the call
A function that can fail says throws. The caller either passes the error on with
? or handles it with catch; there is no third, silent option.
def parsePort(s: str): int throws
var n = s.tryInt()
if n == nil
raise "not a number: ${s}"
if n < 1 or n > 65535
raise "port out of range: ${n}"
return n
def main()
print(parsePort("8080")?)
var fallback = parsePort("99999") catch 80
print(fallback)
var p = parsePort("http") catch |e| e.message.len
print(p)
8080
80
18
Leave the ? off and the program does not compile:
def half(n: int): int throws
if n % 2 != 0
raise "odd: ${n}"
return n / 2
def main()
var h = half(10)
print(h)
errors_bad.zbr:7: error: throws call needs '?' (auto error-propagation was removed; add '?')
Lists, lambdas and loops
Collections carry the usual functional methods. A lambda is def(x) = expr,
and for i, x in xs gives the index first.
def main()
var words = ["zebra", "okapi", "quagga", "giraffe"]
var long: List(str) = words.filter(def(w) = w.len > 5)
var loud: List(str) = long.map(def(w) = w.upper())
print(loud.join(", "))
for i, w in words
print("${i}: ${w}")
QUAGGA, GIRAFFE
0: zebra
1: okapi
2: quagga
3: giraffe
Tests beside the code
Any function whose name starts with test_ is a test, in the same file as
the code it tests. zebra test runs them. The third test below is wrong on
purpose, to show what a failure prints: both sides of the comparison.
def slug(title: str): str
return title.trim().lower().replace(" ", "-")
def test_lowercases()
assert slug("Hello World") == "hello-world"
def test_trims()
assert slug(" zebra ") == "zebra"
def test_keeps_digits()
assert slug("Rev 2") == "rev2"
$ zebra test slug.zbr
RUN: test_lowercases
PASS: test_lowercases
RUN: test_trims
PASS: test_trims
RUN: test_keeps_digits
FAIL: test_keeps_digits: assert failed at slug.zbr:11: left == right -- left: "rev-2", right: "rev2"
2 passed, 1 failed
The run exits 1 when any test fails, so it can gate a build. A test can be
tagged and run on its own, and zebra test --coverage writes line coverage
for the run.