Skip to content

Commit 05f8d69

Browse files
authored
Merge pull request #315 from Eggsrael/main
2 parents 858ba52 + 7e772cd commit 05f8d69

File tree

1 file changed

+92
-3
lines changed

1 file changed

+92
-3
lines changed

docs/writing-reactors/preambles.mdx

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,98 @@ The important takeaway here is with the package.json file and the compiled JavaS
440440

441441
<ShowIf rs>
442442

443-
:::danger
444-
FIXME: Add `preamble` example.
445-
:::
443+
For example, you can use the `preamble` to define macros and functions that are shared within the given reactor:
444+
445+
```lf-rs
446+
target Rust;
447+
448+
main reactor {
449+
preamble {=
450+
macro_rules! add {
451+
($a:expr, $b:expr) => {
452+
$a+$b
453+
}
454+
}
455+
fn add_42 (a: i32) -> i32 {
456+
return a + 42;
457+
}
458+
=}
459+
460+
reaction(startup) {
461+
println!("add macro example: {}", add!(1,1));
462+
println!("add 42 example: {}", add_42(10));
463+
}
464+
}
465+
```
466+
This will print:
467+
468+
```
469+
add macro example: 2
470+
add 42 example: 52
471+
```
472+
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+
477+
For example, the follow reactor brings the constant `PI` into scope and prints out the first 15 digits of pi:
478+
479+
```lf-rs
480+
target Rust;
481+
482+
main reactor {
483+
preamble {=
484+
use std::f64::const::PI;
485+
=}
486+
487+
reaction(startup) {=
488+
println!("{}", PI);
489+
=}
490+
}
491+
```
492+
493+
This will print:
494+
495+
```
496+
3.141592653589793
497+
```
498+
By putting `use` in the `preamble`, the scope of `PI` is changed for the entire reactor, in this case the main reactor.
499+
This works with all items that are brought into scope using the `use` statement and not just constants and functions.
500+
501+
This would be an example of accessing a user created module named `foo` that is included using the `rust-include` target parameter:
502+
503+
File: example.lf
504+
505+
```lf-rs
506+
target Rust {
507+
rust-include: ["foo.rs"]
508+
}
509+
main reactor {
510+
preamble {=
511+
use crate::foo::world;
512+
=}
513+
reaction(startup) {
514+
world();
515+
}
516+
}
517+
```
518+
519+
File: foo.rs
520+
521+
```rust
522+
pub fn world() {
523+
println!("Hello, world!");
524+
}
525+
```
526+
527+
This will print:
528+
529+
```
530+
Hello, world!
531+
```
532+
533+
It is important to note that besides [crates](https://doc.rust-lang.org/book/ch07-01-packages-and-crates.html) specified in the rust target declaration's _cargo-dependencies_ and user modules in _rust-include_, all user code is added to a module called `reactors` during compilation time.
534+
This means that crate level attributes like `#![no_std]` will not work if added to the preamble.
446535

447536
</ShowIf>
448537
</ShowIfs>

0 commit comments

Comments
 (0)