Skip to content

Commit 5960df0

Browse files
committed
Docs(preamble): Fixed examples and added explanations
1 parent 2a98f19 commit 5960df0

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

docs/writing-reactors/preambles.mdx

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -440,78 +440,81 @@ The important takeaway here is with the package.json file and the compiled JavaS
440440

441441
<ShowIf rs>
442442

443-
For example, the follow reactor brings the constant `PI` into scope allowing us to call `f64::const::PI` as `PI`
443+
You can use the `preamble` to define macros and functions that are shared within the given reactor:
444444

445445
```lf-c
446446
target Rust;
447447
448-
main reactor {
448+
main reactor {
449449
preamble {=
450-
use std::f64::const::PI;
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+
}
451458
=}
452459
453-
reaction(startup) {=
454-
println!("{}", PI);
455-
=}
460+
reaction(startup) {
461+
println!("add macro example: {}", add!(1,1));
462+
println!("add 42 example: {}", add_42(10));
463+
}
456464
}
457465
```
458-
459-
This will print:
460-
466+
This will print:
461467
```
462-
3.141592653589793
468+
add macro example: 2
469+
add 42 example: 52
463470
```
464-
for this example, we add the crate [hello](https://docs.rs/hello/latest/hello/) through the rust [cargo-dependencies](<../reference/target-declaration#cargo-dependencies>) target option.
465-
```lf-C
466-
target Rust {
467-
cargo-dependencies: {
468-
hello: "1.0.4",
469-
},
470-
};
471471

472-
preamble {=
473-
use hello;
474-
=}
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:
475473

476-
main reactor {
477-
reaction(startup) {
478-
hello::world();
479-
}
480-
}
474+
```lf-c
475+
target Rust;
476+
477+
main reactor {
478+
preamble {=
479+
use std::f64::const::PI;
480+
=}
481481
482+
reaction(startup) {=
483+
println!("{}", PI);
484+
=}
485+
}
482486
```
483487
This will print:
484488
```
485-
Hello, world!
489+
3.141592653589793
486490
```
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.
487492

488-
In this example we create a function `add_42` and a rust macro `add!`
489-
```lf-c
490-
target Rust;
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:
491494

492-
main reactor {
495+
File: example.lf
496+
```lf-c
497+
target Rust {
498+
rust-include: ["foo.rs"]
499+
}
500+
main reactor {
493501
preamble {=
494-
macro_rules! add {
495-
($a:expr, $b:expr) => {
496-
$a+$b
497-
}
498-
}
499-
fn add_42 (a: i32) -> i32 {
500-
return a + 42;
501-
}
502+
use crate::foo::world;
502503
=}
503-
504-
reaction(startup) {
505-
println!("add macro example: {}", add!(1,1));
506-
println!("add 42 example: {}", add_42(10));
504+
reaction(startup) {
505+
world();
507506
}
508507
}
509-
510508
```
511-
This will print:
509+
File: foo.rs
510+
```rust
511+
pub fn world() {
512+
println!("Hello, world!");
513+
}
512514
```
513-
add macro example: 2
514-
add 42 example: 52
515+
This will print:
516+
```
517+
Hello, world!
515518
```
516519

517520
:::danger

0 commit comments

Comments
 (0)