Skip to content

Types

let text: Str = "Hello, World!"
let number: Int = 42
let decimal: Float = 3.14
let flag: Bool = true
// Lists
let numbers: [Int] = [1, 2, 3, 4, 5]
let names: [Str] = ["Alice", "Bob", "Charlie"]
// Maps
let scores: [Str:Int] = ["Alice": 95, "Bob": 87]
let config: [Int:Str] = [0: "zero", 1: "one", 2: "two"]

Void is another built-in primitive that represents non-existence. It’s rarely used in Ard except as a placeholder to signal an impossibility (Similar to never in Typescript).

The compiler can infer types from context:

let count = 42
let items = [1, 2, 3]
let person = ["name": "Alice", "age": 30]

Type unions allow a value to be one of several types:

type Printable = Str | Int
type Value = Int | Float | Str
let item: Printable = "Hello"
let data: Value = 42

Use match expressions to handle different types in a union:

type Content = Str | Int | Bool
fn describe(value: Content) Str {
match value {
Str => "Text: {it}"
Int => "Number: {it.to_str()}"
Bool => "Flag: {it.to_str()}"
}
}
let items: [Content] = ["hello", 42, true]
for item in items {
io::print(describe(item))
}

The it variable is automatically bound to the matched value.

Use the ? suffix after a type to declare the possibility of it not being present.

use ard/maybe
mut maybe_name: Str? = maybe::none()
maybe_name = maybe::some("Alice")
// Pattern matching
match maybe_name {
name => "Hello, {name}!"
_ => "Hello, stranger!"
}
// Checking presence
if maybe_name.is_some() {
io::print("Name is present")
}
if maybe_name.is_none() {
io::print("No name provided")
}
// Providing defaults
let name: Str = maybe_name.or("Anonymous")
let maybe_value: Int? = maybe::some(42)
// Check if value is present
let has_value: Bool = maybe_value.is_some()
let is_empty: Bool = maybe_value.is_none()
// Get value or default
let value: Int = maybe_value.or(0)

Use a $ prefix on a type to indicate a generic (unspecfied type).

fn identity(value: $T) $T {
value
}