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
- Split mapping/storage interface examples into two files so the
interface lives with its owning program and call sites are clearly
cross-program.
- Drop blow-by-blow list of frontend passes in libraries.md in favor
of "semantic validation".
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: documentation/language/06_libraries.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -183,7 +183,7 @@ Explicit disambiguation using absolute paths (similar to Rust's `crate::foo::…
183
183
184
184
## Building a Library
185
185
186
-
Running `leo build` inside a library package parses the library sources and runs the full frontend pipeline — name validation, global-item collection, path resolution, interface checks, type checking, and static analysis — on the library itself. Type errors, unknown identifiers, interface-cycle errors, and the like are reported at the library package, instead of surfacing only when a downstream program consumes it.
186
+
Running `leo build` inside a library package parses the library sources and runs semantic validation on the library itself. Type errors, unknown identifiers, interface-cycle errors, and the like are reported at the library package, instead of surfacing only when a downstream program consumes it.
These reads are only valid inside a `final fn` or a `final {}` block — they lower to the AVM `get.dynamic`, `contains.dynamic`, and `get.or_use.dynamic` instructions. Dynamic *writes* are not supported.
206
206
207
-
```leo
207
+
`bank.aleo` declares the `Bank` interface and implements it:
208
+
209
+
```leo title="bank/src/main.leo"
208
210
interface Bank {
209
211
mapping balances: address => u64;
210
212
}
211
213
214
+
program bank.aleo: Bank {
215
+
mapping balances: address => u64;
216
+
217
+
fn deposit(user: address, amount: u64) -> Final {
218
+
return final { do_deposit(user, amount); };
219
+
}
220
+
221
+
@noupgrade
222
+
constructor() {}
223
+
}
224
+
225
+
final fn do_deposit(user: address, amount: u64) {
226
+
let prev: u64 = Mapping::get_or_use(balances, user, 0u64);
227
+
Mapping::set(balances, user, prev + amount);
228
+
}
229
+
```
230
+
231
+
A second program imports `bank.aleo` and reads its mapping through the interface. Since the read is cross-program, the interface name is qualified with `bank.aleo::`:
When the interface is in scope without a program qualifier (for example, self-reading from the program that declares it), the leading `program.aleo::` segment can be omitted: `Bank@(target)::balances.get(key)`.
254
+
When the reader is inside the same program that declares the interface, drop the program qualifier — `Bank@(target)::balances.get(key)` rather than `bank.aleo::Bank@(target)::balances.get(key)`.
Singleton storage is read by naming the variable directly (no trailing `.op(...)`). Vector storage supports `.get(index)` (out-of-bounds reads return `none`) and `.len()` (no arguments). Storage writes through the dynamic interface are not supported — use a dynamic call to an entry function that performs the write.
245
267
246
-
```leo
268
+
`logger.aleo` declares the `Logger` interface and implements it:
269
+
270
+
```leo title="logger/src/main.leo"
247
271
interface Logger {
248
272
storage counter: u64;
249
273
storage entries: [u64];
250
274
}
251
275
276
+
program logger.aleo: Logger {
277
+
storage counter: u64;
278
+
storage entries: [u64];
279
+
280
+
fn bump(val: u64) -> Final {
281
+
return final {
282
+
counter = counter.unwrap_or(0u64) + 1u64;
283
+
entries.push(val);
284
+
};
285
+
}
286
+
287
+
@noupgrade
288
+
constructor() {}
289
+
}
290
+
```
291
+
292
+
A second program imports `logger.aleo` and reads its storage variables through the interface. Since the read is cross-program, the interface name is qualified with `logger.aleo::`:
0 commit comments