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: docs/writing-reactors/preambles.mdx
+18-3Lines changed: 18 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -464,12 +464,16 @@ main reactor {
464
464
}
465
465
```
466
466
This will print:
467
+
467
468
```
468
469
add macro example: 2
469
470
add 42 example: 52
470
471
```
471
472
472
-
By having the `preamble` inside the reactor, you can define any functions or macros that are shared within the given reactor. This will allow you to reuse code across different reactions easily within the same reactor. This also can extend to `use` to shorten declaration of module items. For example, the follow reactor brings the constant `PI` into scope and print out the first 15 digits of pi:
473
+
By having the `preamble` inside the reactor, you can define any functions or macros to be shared within the given reactor.
474
+
This will allow you to reuse code across different reactions easily within the same reactor.
475
+
This also can allow us to change the scope of items using the 'use' statement.
476
+
For example, the follow reactor brings the constant `PI` into scope and print out the first 15 digits of pi:
473
477
474
478
```lf-c
475
479
target Rust;
@@ -484,15 +488,19 @@ main reactor {
484
488
=}
485
489
}
486
490
```
491
+
487
492
This will print:
493
+
488
494
```
489
495
3.141592653589793
490
496
```
491
-
By putting `use` in the `preamble`, you can change the scope of `PI` for the for the reactor, in this case the main reactor. Its important to note that besides the crates specified in the target declarations _cargo-dependencies_ and user modules in _rust-include_, all user code is added to a module called `reactors` during compilation time. This means that crate level attributes like `#![no_std]` will not work if added to the preamble.
497
+
By putting `use` in the `preamble`, the scope of `PI` is changed for the entire reactor, in this case the main reactor.
498
+
This works with all items that are brought into scope using the `use` statement and not just constants and functions.
492
499
493
-
This would be an example of accessing a user created module named `foo` that is in the same directory as the lingua france program:
500
+
This would be an example of accessing a user created module named `foo` that is included using the `rust-include` target parameter:
494
501
495
502
File: example.lf
503
+
496
504
```lf-c
497
505
target Rust {
498
506
rust-include: ["foo.rs"]
@@ -506,17 +514,24 @@ main reactor {
506
514
}
507
515
}
508
516
```
517
+
509
518
File: foo.rs
519
+
510
520
```rust
511
521
pubfnworld() {
512
522
println!("Hello, world!");
513
523
}
514
524
```
525
+
515
526
This will print:
527
+
516
528
```
517
529
Hello, world!
518
530
```
519
531
532
+
Its important to note that besides the crates specified in the target declarations _cargo-dependencies_ and user modules in _rust-include_, all user code is added to a module called `reactors` during compilation time.
533
+
This means that crate level attributes like `#![no_std]` will not work if added to the preamble.
0 commit comments