Skip to content
This repository was archived by the owner on May 4, 2026. It is now read-only.

Commit 3265a61

Browse files
author
Mohammad Fawaz
committed
const docs
1 parent 8fc7e1e commit 3265a61

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

documentation/language/02_structure.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ The following must be declared inside the scope of a program in a Leo file:
6464
The following must be declared outside the scope of a program in a Leo file:
6565

6666
- imports
67-
- constants
6867
- struct types
6968
- helper `fn` definitions
7069
- `final fn` definitions
@@ -91,21 +90,42 @@ program _foo.aleo; // invalid
9190

9291
### Constant
9392

94-
A constant is declared as `const {name}: {type} = {expression};`.
95-
Constants are immutable and must be assigned a value when declared.
96-
Constants can be declared in the global scope or in a local function scope.
93+
A constant is declared as `const {name}: {type} = {expression};`.
94+
Constants are immutable, and the right-hand side must be an expression evaluatable at compile time.
95+
96+
Constants can be declared in three scopes:
97+
98+
- **Global scope** (outside all program blocks): accessible anywhere in the file.
99+
- **Program scope** (inside a `program` block, outside any function): accessible within that program.
100+
- **Local scope** (inside a function body): accessible only within that function.
97101

98102
```leo
99-
const FOO: u8 = 1u8;
103+
const MAX: u64 = 100u64; // global constant
100104
101105
program foo.aleo {
102-
fn bar() -> u8 {
103-
const BAR: u8 = 2u8;
104-
return FOO + BAR;
106+
const MULTIPLIER: u64 = 2u64; // program-scope constant
107+
108+
fn compute(x: u64) -> u64 {
109+
const OFFSET: u64 = 5u64; // local constant
110+
return x * MULTIPLIER + OFFSET;
105111
}
106112
}
107113
```
108114

115+
**Supported types:** All integer types (`u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64`, `i128`), `bool`, `field`, `group`, `scalar`, `address`, and tuples, arrays, and structs composed of these types.
116+
117+
**Compile-time expressions:** The right-hand side of a constant declaration must be evaluatable at compile time. Valid right-hand sides include:
118+
- Literal values (e.g., `42u32`, `true`, `1field`)
119+
- References to previously declared constants
120+
- Arithmetic, bitwise, and comparison expressions over constants (e.g., `MAX * 2u64`, `!FLAG`)
121+
- Tuple, array, and struct expressions whose components are themselves compile-time constants
122+
123+
```leo
124+
const BASE: u32 = 10u32;
125+
const LIMIT: u32 = BASE * 5u32; // expression over constants
126+
const PAIR: (u32, bool) = (LIMIT, true); // tuple constant
127+
```
128+
109129
### Import
110130

111131
You can import dependencies that are downloaded to the `imports` directory.

0 commit comments

Comments
 (0)