Enums and options¶
enum Shape:
Circle(float)
Rect(float, float)
Empty
fn area(s: Shape) -> float:
match s:
case Shape.Circle(r):
ret 3.0 * r * r
case Shape.Rect(w, h):
ret w * h
case Shape.Empty:
ret 0.0
for s in [Shape.Circle(1.0), Shape.Rect(2.0, 3.0), Shape.Empty]:
print(s, area(s))
fn find(names: list[str], name: str) -> option[int]:
for i in range(names.len()):
if names[i] == name:
ret some(i)
ret none
names = ["ada", "bo"]
match find(names, "bo"):
case some(i):
print("bo is at", i)
case none:
print("bo is missing")
print(find(names, "cy"), find(names, "ada").unwrap_or(-1))
enum Command:
Move(dx: int, dy: int)
Say(text: str)
Quit
fn run(c: Command) -> str:
match c:
case Command.Move(dx=0, dy=0):
ret "stay"
case Command.Move(dx, dy):
ret "move by {dx}, {dy}"
case Command.Say(text="hi" | "hello"):
ret "greet"
case Command.Say(text):
ret "say {text}"
case Command.Quit:
ret "stop"
for c in [Command.Move(dy=2, dx=1), Command.Move(dx=0, dy=0), Command.Say(text="hi"), Command.Quit]:
print(c, run(c))
fn size(n: int) -> str:
match n:
case 0:
ret "none"
case 1 | 2 | 3:
ret "a few"
case _:
ret "many"
print(size(0), size(2), size(10))
prints
Shape.Circle(1.0) 3.0
Shape.Rect(2.0, 3.0) 6.0
Shape.Empty 0.0
bo is at 1
none 0
Command.Move(dx=1, dy=2) move by 1, 2
Command.Move(dx=0, dy=0) stay
Command.Say(text='hi') greet
Command.Quit stop
none a few many
Enums¶
An enum is a type whose values are one of a few kinds, called its variants. enum Shape: opens
one, and each indented line after it names a variant and, in parentheses, the types of the
values it holds, so a Shape is a circle with a radius, a rectangle with a width and a height,
or empty. Enums are like Rust’s: a variant can hold any values, including lists and other enum
values, or none at all.
A variant is written after its enum’s name: Shape.Circle(1.0) makes a circle, and
Shape.Empty the empty shape. Like a tuple, an enum value never changes, and it prints the way
it is written.
Match¶
match looks at a value and runs the first case whose pattern fits it. case Shape.Rect(w, h):
fits a rectangle and names its two values w and h for the block under it, and _ in place
of a name fits any value without naming it, as in case Shape.Rect(w, _):. A name on its own,
as in case other:, fits every value.
The cases must cover every value. Leave out case Shape.Empty: from area, and the checker
stops before the program runs:
error[E0027]: 'match' does not cover Shape.Empty; add 'case Shape.Empty:' or 'case _:'
So adding a variant to an enum points at every match that needs a case for it.
Named values¶
A variant can name its values instead, as Move(dx: int, dy: int) does in Command. Its values
are then given by name, in any order, as a struct’s are: Command.Move(dy=2, dx=1). A variant
names all of its values or none, and a named one prints with its names, as
Command.Move(dx=1, dy=2).
Its patterns name the values too. Command.Move(dx=0, dy=0) fits a move whose dx and dy are
both 0, and Command.Move(dx, dy) is short for Command.Move(dx=dx, dy=dy), naming each value
after its field. A value the pattern leaves out fits anything, so case Command.Move: alone fits
every move.
More patterns¶
A pattern can be an int, a string, or a bool, which fits a value equal to it, as 0 and
"hi" do above. | joins patterns, any of which may fit, as in case 1 | 2 | 3:, and it works
inside a variant too: Command.Say(text="hi" | "hello"). Every pattern joined by | names the
same values.
match takes an int, a string, or a bool as well as an enum. There are too many ints and
strings for cases to list them all, so a match on one ends with a case that fits any value,
such as case _:. A float is never a pattern, since nan equals nothing; compare floats with
if.
Options¶
Some answers can be missing, such as the position of a name that is not in a list. stone has no
null: a value that may be missing is an option, option[T], which is either some(x), holding a
T, or none. find above returns an option[int], and match reads it with the patterns
some(i) and none, so the program cannot use a position that is not there.
option is not a special type: it is an enum like Shape, which every program gets from a
small stone file called the prelude, and whose variants are written alone, as some(x) rather
than option.some(x). It holds any type T, which Generics shows how to write for your
own enums.
An option also has methods: unwrap_or(-1) gives the value or -1 when there is none,
is_some() and is_none() say which it is, and unwrap() gives the value, stopping the
program with cannot unwrap none when there is none. Options compare with == and !=, so
x == some(3) and x != none work too, as do values of your own enums. See
Types for the details.
Next, Generics.