6. Documentation
Types
Braid has a strong static type system with Hindley-Milner type inference. Every expression has a type known at compile time.
Primitive Types
| Type | Description | Range / Values |
|---|---|---|
int | 64-bit signed integer | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 64-bit double precision IEEE 754 | ±4.94e-324 to ±1.798e+308 |
bool | Boolean | true or false |
string | UTF-8 string (heap-allocated, ARC-managed) | Variable length |
let a: int = 42
let b: float = 3.14159
let c: bool = true
let d: string = "hello"Compound Types
Struct
User-defined composite types with named fields:
struct Point {
x: int
y: int
}
let p = Point { x: 10, y: 20 }Enum
Named variants with no associated data:
enum Color {
Red
Green
Blue
}
let c = Color.RedArray
Dynamic homogeneous arrays:
let arr = [1, 2, 3, 4, 5]
let first = arr[0]Tensor
N-dimensional arrays for ML workloads:
let matrix = [[1, 2], [3, 4]]
let val = matrix[0][1] // 2
let slice = matrix[0:2] // rows 0-1Tensor dtypes include: float64, float32, float16, bfloat16, int8, int16, int32, int64, ternary.
Special Types
Diameter
Dialectical programming construct modeling two opposing poles:
diameter temperature: {
pole hot: { return 80.0 }
pole cold: { return 10.0 }
}Function
Functions are first-class values:
fn add(x: int, y: int) -> int {
return x + y
}
let double = fn(x: int) -> int { return x * 2 }Model
ML model configuration type:
model my_llm = Transformer {
vocab_size: 50257;
d_model: 512;
}Optional Types
Braid uses nil to represent the absence of a value:
fn divide_safe(a: float, b: float) {
if b == 0.0 {
return nil
}
return a / b
}
let result = divide_safe(10.0, 0.0)
if result == nil {
print("Division by zero!")
}Type Annotations
Type annotations use : TypeName syntax after identifiers:
let x: int = 5
let y: float = 3.14
let name: string = "Braid"
fn add(a: int, b: int) -> int {
return a + b
}Type Checker
The type checker uses Hindley-Milner unification:
- Type variables — unknown types resolved through unification
- Unification — merges two types, creating parent links for type variables
- Generic types —
Type<SubType>syntax supported in parser - Comparison operators always return
bool - Logical operators require
booloperands