Skip to content

Enums and match

A struct holds all its fields at once. An enum holds one of its cases: a traffic light is red, orange or green, never two at a time.

enum Light {
Red
Orange
Green
fn next(self) -> Light {
return match self {
.Red => Light.Green
.Orange => Light.Red
.Green => Light.Orange
}
}
}
fn main {
let light := Light.Red
print(light.next)
print(light.next.next)
}
Light.Green
Light.Orange

A case is written Light.Red; where the type is already known, as in a match on a Light, just .Red. An enum may have methods and static functions, like a struct.

Each case may hold its own fields. A shape is a circle with a radius, a rectangle with two sides, or a dot with nothing:

enum Shape {
Circle(radius: Float)
Rect(width: Float, height: Float)
Dot
}
fn area(s: Shape) -> Float {
return match s {
.Circle(r) => 3.14159 * r * r
.Rect(w, h) => w * h
.Dot => 0.0
}
}
fn main {
let shapes: List<Shape> := [Shape.Circle(1.0), Shape.Rect(2.0, 3.0), Shape.Dot]
for s in shapes {
print(area(s))
}
}
3.14159
6.0
0.0

In a case, Circle(r) names the case’s fields in their order: r is the radius, and exists only inside that case. _ stands for a field that is not needed: Rect(w, _).

This is how Kova says “one of these, and here is what each one holds” in one type. Other languages need a class per case and a type test, or a struct with fields that only mean something for some values; the compiler then cannot tell which field is valid when.

When the enum is already known from where the value goes, the case starts with a dot:

enum Light {
Red
Green
}
fn name(l: Light) -> String {
return match l {
.Red => "stop"
.Green => "go"
}
}
fn main {
let l: Light := .Green
print(name(l))
print(name(.Red))
print(l = .Green)
}
go
stop
true

.Green works everywhere a Light is expected: a variable with its type, an argument, a return, a field, a list of Light, the other side of =. Where nothing says which enum it is, such as print(.Red), Kova asks for the full name.

The compiler knows every case of an enum, so it checks that a match handles them all:

enum Shape {
Circle(radius: Float)
Rect(width: Float, height: Float)
Dot
}
fn area(s: Shape) -> Float {
return match s {
.Circle(r) => 3.0 * r * r
.Rect(w, h) => w * h
}
}
fn main {
print(area(Shape.Dot))
}
error: cases of `Shape` not handled: `.Dot`
--> main.kova:8:18
|
8 | return match s {
| ^
= help: add them, or end with `_ => …` for all the others

The day a case is added to an enum, every match that forgets it is shown, instead of a program that silently does nothing for it. In the editor, a quick fix writes the missing cases.

_ => … handles all the cases not written; use it when they really are all treated the same, since it also hides the cases added later. Several cases with no fields can share a line:

fn main {
let today := Date.year(2026) month(10) day(24)
match today.weekday {
.Saturday, .Sunday => print("weekend")
_ => print("work")
}
}
weekend

Cases with fields can share a line too, when each names the same names, of the same type: the case found gives its fields to them.

enum Shape {
Circle(radius: Float)
Square(side: Float)
Rect(width: Float, height: Float)
}
fn widest(s: Shape) -> Float {
return match s {
.Circle(x), .Square(x) => x
.Rect(w, h) => if w > h { w } else { h }
}
}
fn main {
print(widest(Shape.Square(3.0)))
print(widest(Shape.Rect(1.0, 4.0)))
}
3.0
4.0

.Circle(x), .Rect(x, _) works as well: what counts is the names, not where the fields are. A name missing from one case (.Circle(x), .Point) is refused, since x would have no value there.

Like if, match gives the value of the case taken, as in area above. A case may also run a whole block:

enum Command {
Say(text: String)
Quit
}
fn main {
let commands: List<Command> := [Command.Say("hello"), Command.Quit, Command.Say("never")]
for c in commands {
match c {
.Say(t) => print(t)
.Quit => {
print("bye")
break
}
}
}
}
hello
bye

A case may hold another case where a value goes: err(.Lost) is “an error, and that error is Lost”. It saves a match inside a match:

enum Problem {
Lost
Late(minutes: Int)
}
fn fetch(k: Int) -> Int or Problem {
if k = 0 {
return err(.Lost)
}
if k = 1 {
return err(.Late(5))
}
return ok(k * 10)
}
fn main {
for k in 0..4 {
match fetch(k) {
ok(20) => print("twenty")
ok(v) => print("got {v}")
err(.Lost) => print("lost")
err(.Late(m)) => print("late by {m}")
}
}
}
lost
late by 5
twenty
got 30

The cases of one variant are checked together, so every case inside must be handled too. A later err(e) => takes the others and names the whole error; a final _ => takes them as well:

enum Problem {
Lost
Late(minutes: Int)
}
fn main {
let r: Int or Problem := err(.Late(3))
match r {
ok(v) => print(v)
err(.Lost) => print("lost")
}
}
error: cases of `Problem` not handled: `.Late`
--> main.kova:10:9
|
10 | err(.Lost) => print("lost")
| ^^^
= help: add them, or end with `_ => …` for all the others

A variant of several fields takes values and variants without fields among its names, and so does a tuple:

enum Light {
Red
Green
}
enum Event {
Pair(light: Light, n: Int)
Quit
}
fn main {
let events: List<Event> := [.Pair(.Red, 1), .Pair(.Red, 3), .Pair(.Green, 2), .Quit]
for e in events {
match e {
.Pair(.Red, 1) => print("red one")
.Pair(.Red, n) => print("red {n}")
.Pair(_, n) => print("green {n}")
.Quit => print("quit")
}
}
match (Light.Green, 2) {
(.Red, _) => print("red")
(.Green, k) => print("green {k}")
_ => print("never")
}
}
red one
red 3
green 2
quit
green 2

Kova reads one level: err(.Lost) and .Plain(.Circle(r)) work, ok(.Pair(.Red, n)) does not. With several fields, a case inside is a value or a variant without fields (.Pair(.Late(m), 1) is refused: name the field and match on it).

A case may hold a value of its own enum. This is how trees and expressions are written:

enum Expr {
Num(value: Int)
Add(left: Expr, right: Expr)
Mul(left: Expr, right: Expr)
}
fn eval(e: Expr) -> Int {
return match e {
.Num(v) => v
.Add(a, b) => eval(a) + eval(b)
.Mul(a, b) => eval(a) * eval(b)
}
}
fn main {
let e := Expr.Add(Expr.Num(2), Expr.Mul(Expr.Num(3), Expr.Num(4)))
print(eval(e))
}
14

Enums can also be generic, enum Tree<T> { Leaf; Node(left: Tree<T>, value: T, right: Tree<T>) } (Generics).

The names of a case are read-only. To change the value in place, match it with var:

enum Counter {
Off
On(count: Int)
}
fn tick(c: var Counter) {
match var c {
.On(n) => n := n + 1
.Off => {}
}
}
fn main {
var c := Counter.On(1)
tick(var c)
tick(var c)
print(c)
}
Counter.On(3)

Two enums are always there: T? (a value or none) and T or E (a result or an error), the subject of the next chapter but one.