Skip to content
Unverified: This page has not yet been anchored to automated code tests.

How-To: Model Complex Domain Logic

Learn how to use Vox’s expressive type system to model your application’s domain logic effectively.

Vox supports powerful ADTs (sum types) for representing state that can be one of several variants.

type OrderStatus =
| Pending
| Processing(staff_id: str)
| Shipped(tracking_number: str)
| Delivered(timestamp: int)

Use the match expression to handle ADT variants with full type safety.

fn describe_status(status: OrderStatus) to str {
return match status {
Pending => "Waiting for staff"
Processing(id) => "Being handled by " + id
Shipped(track) => "In transit: " + track
Delivered(ts) => "Package reached destination"
}
}

Group related data into named structs.

type Address {
street: str
city: str
zip: int
}
type Customer {
name: str
email: str
shipping_address: Address
}

Add runtime guards to your data types using the @require decorator.

// vox:skip -- illustrative future syntax; @require only decorates fn today, not type
@require(len(self.password) > 8)
type UserAccount {
username: str
password: str
}
  • Describe mutually exclusive states and data variants cleanly using ADTs (Sum Types).
  • Avoid invalid states with constructor validation guards via @require.
  • Pattern match to strictly process all possibilities at compile time.