Variables & Types
AodhML uses explicit type annotations with inference where unambiguous. Variables are immutable by default; mutability is opt-in.
Variable Declaration
Use let for immutable variables and mut for mutable ones:
let name = "AodhML" // Immutable, type inferred as String
let count: i32 = 42 // Immutable with explicit type
mut counter = 0 // Mutable, can be reassigned
counter = counter + 1 // OK: counter is mutable
// name = "Other" // Error: name is immutable
Scalar Types
| Type | Description | Example |
|---|---|---|
i8, i16, i32, i64 | Signed integers | let x: i32 = -42 |
u8, u16, u32, u64 | Unsigned integers | let x: u32 = 42 |
f32, f64 | Floating-point | let x: f64 = 3.14159 |
bool | Boolean | let ok = true |
String | UTF-8 string | let s = "hello" |
Type Inference
The compiler infers types from context when possible:
let inferred = 42 // i32
let precise: f64 = 42.0 // f64 explicitly
let tensor = Tensor::zeros([3, 3]) // Tensor<f32>
Type Aliases
Create named type aliases with the type keyword:
type Vector = Tensor<f32>
type Matrix = Tensor<f32>
fn dot(a: Vector, b: Vector) -> f32 {
// ...
}
Optionals
Values that may be absent use the Option<T> type:
fn find_index(arr: [i32], target: i32) -> Option<u32> {
for i, val in arr {
if val == target {
return Some(i)
}
}
return None
}