Overview
Linea uses a clean, modern syntax for variable declarations with the @ operator for type annotations. This design makes code more readable and reduces cognitive overhead.
Basic Syntax
Simple Variable Declaration
The fundamental pattern for variable declaration in Linea:
var name @ type = value
Example: Basic Variables
var x @ int = 42
var name @ str = "Alice"
var pi @ float = 3.14159
var active @ bool = true
Primitive Types
| Type | Description | Example | Range/Size |
|---|---|---|---|
int |
Integer (64-bit signed) | 42, -100 |
-2^63 to 2^63-1 |
i32 |
32-bit integer | 100 |
-2^31 to 2^31-1 |
i64 |
64-bit integer (explicit) | 999999 |
-2^63 to 2^63-1 |
float |
Floating point (64-bit) | 3.14, 1.5e10 |
IEEE 754 double |
f32 |
32-bit float | 2.5 |
IEEE 754 single |
str, string |
Unicode text string | "Hello" |
Variable length |
bool |
Boolean true/false | true, false |
1 bit |
Composite Types
Arrays
Arrays are fixed-size, homogeneous collections:
var nums @ [int] = [1, 2, 3, 4, 5]
var matrix @ [float] = [[1.0, 2.0], [3.0, 4.0]]
var names @ [str] = ["Alice", "Bob"]
Vectors (Dynamic Arrays)
Vectors are dynamic arrays that grow as needed:
var dynamic @ Vector<int> = [1, 2, 3]
Matrices
Matrices are 2D arrays, ideal for ML operations:
var mat @ Matrix = [[1.0, 2.0], [3.0, 4.0]]
Type Inference
Linea can infer types from values, but explicit type annotations are recommended for clarity:
Type Inference (Not Recommended)
var x = 42 // Inferred as int
var name = "Alice" // Inferred as str
Pointer Types
Linea v4.1 introduces simplified pointer syntax with the ptr datatype:
Ptr Datatype (v4.1+)
The ptr type provides a clean way to work with pointers:
var x @ int = 42
var ptr_x @ ptr = x // Automatically captures address
Pointer Handles in Practice
Use ptr handles consistently in examples and projects:
var count @ int = 99
var count_ptr @ ptr = count
display "Pointer handle ready"
Variable Updates
Update a variable's value with the assignment operator:
var counter @ int = 0
counter = 10 // Update value
counter = counter + 1 // Increment
Constants
Define constants using the const keyword:
const PI @ float = 3.14159
const MAX_SIZE @ int = 1000
Scope and Lifetime
Variables are block-scoped and follow Linea's automatic memory management:
var outer @ int = 10
{
var inner @ int = 20
display inner // Works: 20
}
// inner is no longer in scope here
Type Conversion
Convert between compatible types using constructors:
var num_int @ int = 42
var num_float @ float = float(num_int) // Convert to float
var text @ str = str(100) // Convert int to string
Common Patterns
ML Training Variable Setup
var learning_rate @ float = 0.001
var epochs @ int = 100
var weights @ Matrix = [[0.1, 0.2], [0.3, 0.4]]
var trained @ bool = false
Best Practices
- Always use explicit types: Makes code self-documenting
- Use meaningful names:
user_agenotua - Group related variables: Define together for clarity
- Use constants for fixed values: Reduces bugs and improves maintainability
- Prefer specific types: Use
intoveri64unless you need exact size