Algebraic Data Types
Kira supports sum types (tagged unions) and product types (records).
Sum Types
Sum types represent a value that can be one of several variants:
// Simple enum
type Color =
| Red
| Green
| Blue
// With associated data
type Shape =
| Circle(f64)
| Rectangle(f64, f64)
| Triangle(f64, f64, f64)Generic Sum Types
type Option[T] =
| Some(T)
| None
type Result[T, E] =
| Ok(T)
| Err(E)
type List[T] =
| Cons(T, List[T])
| NilCreating Sum Type Values
let color: Color = Red
let shape: Shape = Circle(5.0)
let opt: Option[i32] = Some(42)
let list: List[i32] = Cons(1, Cons(2, Cons(3, Nil)))
let success: Result[i32, string] = Ok(100)
let failure: Result[i32, string] = Err("Something went wrong")Product Types (Records)
Product types are records with named fields:
type Point = {
x: f64,
y: f64
}
type User = {
id: i64,
name: string,
email: string,
active: bool
}Creating and Accessing Records
let origin: Point = Point { x: 0.0, y: 0.0 }
let p: Point = Point { x: 3.0, y: 4.0 }
// Access fields with dot notation
let x_coord: f64 = p.x
let y_coord: f64 = p.y
let user: User = User {
id: 1,
name: "Alice",
email: "alice@example.com",
active: true
}Why ADTs?
Algebraic data types let you model your domain precisely:
- Sum types prevent impossible states — a
Shapeis exactly one of the defined variants - Product types bundle related data with named fields
- Combined with pattern matching, they enable safe, exhaustive handling of all cases