Types
Primitive Types
Section titled “Primitive Types”| Type | Description | Example |
|---|---|---|
Int |
64-bit signed integer | 42 |
Float |
64-bit floating point | 3.14 |
Bool |
Boolean | true, false |
String |
UTF-8 string | "hello" |
Char |
Single Unicode character | 'z' |
Void |
No value (unit type) | — |
Composite Types
Section titled “Composite Types”Ordered, homogeneous collections:
let numbers: List[Int] = [1, 2, 3, 4, 5]let names = ["Alice", "Bob", "Charlie"] // inferred as List[String]
print(numbers[0]) // 1print(names.len()) // 3Key-value associations:
let ages: Map[String, Int] = { "Alice": 30, "Bob": 25,}
print(ages["Alice"]) // 30Tuples
Section titled “Tuples”Fixed-size, heterogeneous groupings:
let point: (Int, Int) = (10, 20)let (x, y) = point // destructuringStructs
Section titled “Structs”Named product types with fields:
struct User { name: String, email: String, age: Int,}
let user = User { name: "Alice", email: "alice@example.com", age: 30,}Sum types (tagged unions):
enum Shape { Circle(Float), Rectangle(Float, Float), Triangle(Float, Float, Float),}
let s = Shape.Circle(5.0)Option and Result
Section titled “Option and Result”Zap has no null. Instead, use built-in types:
// Option — a value that might not existlet found: Option[User] = find_user("alice")
match found { Some(user) => print(user.name), None => print("Not found"),}
// Result — an operation that might faillet data: Result[String, Error] = read_file("config.toml")
match data { Ok(content) => print(content), Err(e) => print("Error: " + e.message),}Generics
Section titled “Generics”Functions and types can be parameterised:
fn first[T](list: List[T]) -> Option[T] { if list.len() > 0 { Some(list[0]) } else { None }}Type Aliases
Section titled “Type Aliases”type UserId = Inttype UserMap = Map[UserId, User]Next Steps
Section titled “Next Steps”Learn about functions and how to use types in practice.