Types
Built-in Types
Section titled “Built-in Types”Primitive Types
Section titled “Primitive Types”let text: Str = "Hello, World!"let number: Int = 42let decimal: Float = 3.14let flag: Bool = true
Collection Types
Section titled “Collection Types”// Listslet numbers: [Int] = [1, 2, 3, 4, 5]let names: [Str] = ["Alice", "Bob", "Charlie"]
// Mapslet 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).
Type Inference
Section titled “Type Inference”The compiler can infer types from context:
let count = 42let items = [1, 2, 3]let person = ["name": "Alice", "age": 30]
Type Unions
Section titled “Type Unions”Type unions allow a value to be one of several types:
type Printable = Str | Inttype Value = Int | Float | Str
let item: Printable = "Hello"let data: Value = 42
Working with Type Unions
Section titled “Working with Type Unions”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.
Nullable Types (Maybe)
Section titled “Nullable Types (Maybe)”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")
Working with Maybe Types
Section titled “Working with Maybe Types”// Pattern matchingmatch maybe_name { name => "Hello, {name}!" _ => "Hello, stranger!"}
// Checking presenceif maybe_name.is_some() { io::print("Name is present")}
if maybe_name.is_none() { io::print("No name provided")}
// Providing defaultslet name: Str = maybe_name.or("Anonymous")
Maybe Type Methods
Section titled “Maybe Type Methods”let maybe_value: Int? = maybe::some(42)
// Check if value is presentlet has_value: Bool = maybe_value.is_some()let is_empty: Bool = maybe_value.is_none()
// Get value or defaultlet value: Int = maybe_value.or(0)
Generic Syntax
Section titled “Generic Syntax”Use a $
prefix on a type to indicate a generic (unspecfied type).
fn identity(value: $T) $T { value}