You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/language/02_structure.md
+39-8Lines changed: 39 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,6 @@ The following must be declared inside the scope of a program in a Leo file:
64
64
The following must be declared outside the scope of a program in a Leo file:
65
65
66
66
- imports
67
-
- constants
68
67
- struct types
69
68
- helper `fn` definitions
70
69
-`final fn` definitions
@@ -91,21 +90,53 @@ program _foo.aleo; // invalid
91
90
92
91
### Constant
93
92
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).
0 commit comments