9. Enums and better match¶
Enums let you represent “one of several shapes” as a real type, and match lets you handle it exhaustively.
A small command enum¶
enum Command:
Add(int)
Remove(int)
List
Coming from Python?
Python’s Enum is mostly “named constants”. If you want variants that carry different data, Python often uses:
strings, dataclass hierarchies, or Union types.
In Incan, enum variants can carry data (like Add(int)), and match gives you an exhaustive, compiler-checked way
to handle every case.
Matching on enums¶
def run(cmd: Command) -> None:
match cmd:
Add(x) => println(f"add {x}")
Remove(x) => println(f"remove {x}")
List => println("list")
def main() -> None:
run(Command.Add(1)) # outputs: add 1
run(Command.Remove(1)) # outputs: remove 1
run(Command.List) # outputs: list
Why this matters¶
Unlike a stringly-typed “command name”, enums:
- prevent typos
- make invalid states unrepresentable
- give you exhaustive
matchchecking
Try it¶
- Add a
Clearvariant and update thematchaccordingly. - Add a
Rename(str)variant and print the new name.
One possible solution
enum Command:
Add(int)
Remove(int)
Rename(str)
Clear
List
def run(cmd: Command) -> None:
match cmd:
Add(x) => println(f"add {x}")
Remove(x) => println(f"remove {x}")
Rename(name) => println(f"rename {name}")
Clear => println("clear")
List => println("list")
Where to learn more¶
- Enums deep dive: Enums
Next¶
Back: 8. Collections and iteration
Next chapter: 10. Models vs classes