Variables
Variables in Nic are declared with let. They can optionally have type annotations.
Basic Declaration
nic
fn main() -> unit {
// With explicit type
let x: i32 = 42;
// With type inference
let y = 100; // inferred as i32
// Declaration without initialization
let z: i32;
z = 50;
println("Variables declared!");
return;
}Type Annotations
Type annotations follow the variable name with a colon:
nic
fn main() -> unit {
let age: i32 = 25;
let pi: f64 = 3.14159;
let name: string = "Alice";
let active: bool = true;
return;
}Mutability
All variables in Nic are mutable by default. You can reassign them freely:
nic
fn main() -> unit {
let count: i32 = 0;
count = 1;
count = 2;
count = count + 1; // count is now 3
return;
}Constants
Use const for compile-time constants that cannot be changed:
nic
const MAX_SIZE: i32 = 1024;
const PI: f64 = 3.14159265359;
fn main() -> unit {
let radius: f64 = 5.0;
let area: f64 = PI * radius * radius;
return;
}Constants must:
- Have an explicit type
- Be initialized with a compile-time constant expression
- Be declared at module level (not inside functions)
Shadowing
You can declare a new variable with the same name, which shadows the previous one:
nic
fn main() -> unit {
let x: i32 = 5;
let x: i32 = x + 10; // shadows previous x
let x: string = "now a string"; // can even change type
return;
}Summary
| Feature | Syntax | Example |
|---|---|---|
| Variable | let name: Type = value; | let x: i32 = 42; |
| Inferred | let name = value; | let x = 42; |
| Constant | const NAME: Type = value; | const MAX: i32 = 100; |
Next
Learn about the Types you can use with variables.