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

Commit 774f99c

Browse files
author
Mohammad Fawaz
committed
const docs
1 parent 8fc7e1e commit 774f99c

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

documentation/language/02_structure.md

Lines changed: 39 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,53 @@ 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 four scopes:
97+
98+
- **Global scope** (outside all program blocks in a program file): accessible anywhere in the same 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.
101+
- **Module scope** (in a module file within the same package, i.e. a `.leo` file with no `program` block): accessible within the same package via `path::to::module::CONST_NAME`. See [Modules](./01_layout.md#modules) for details.
102+
103+
Constants are also supported in [libraries](./06_libraries.md), which are separate packages containing reusable code. A library's root file and its submodules may declare constants, accessible from any dependent package as `library::CONST_NAME` or `library::path::to::submodule::CONST_NAME`.
104+
105+
**Accessibility across packages:** Global and program-scope constants in a program are accessible from other programs that import it, using `program_name.aleo::CONST_NAME`. The following are not yet supported:
106+
107+
1. Accessing a constant from a submodule of an imported program
108+
2. Accessing an imported program's constant from within a submodule of the current program
109+
110+
See [ProvableHQ/leo#29274](https://github.com/ProvableHQ/leo/issues/29274).
97111

98112
```leo
99-
const FOO: u8 = 1u8;
113+
const MAX: u64 = 100u64; // global constant
100114
101115
program foo.aleo {
102-
fn bar() -> u8 {
103-
const BAR: u8 = 2u8;
104-
return FOO + BAR;
116+
const MULTIPLIER: u64 = 2u64; // program-scope constant
117+
118+
fn compute(x: u64) -> u64 {
119+
const OFFSET: u64 = 5u64; // local constant
120+
return x * MULTIPLIER + OFFSET;
105121
}
106122
}
107123
```
108124

125+
**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.
126+
127+
**Compile-time expressions:** The right-hand side of a constant declaration must be evaluatable at compile time. Valid right-hand sides include:
128+
129+
- Literal values (e.g., `42u32`, `true`, `1field`)
130+
- References to previously declared constants
131+
- Arithmetic, bitwise, and comparison expressions over constants (e.g., `MAX * 2u64`, `!FLAG`)
132+
- Tuple, array, and struct expressions whose components are themselves compile-time constants
133+
134+
```leo
135+
const BASE: u32 = 10u32;
136+
const LIMIT: u32 = BASE * 5u32; // expression over constants
137+
const PAIR: (u32, bool) = (LIMIT, true); // tuple constant
138+
```
139+
109140
### Import
110141

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

0 commit comments

Comments
 (0)