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
+49-46Lines changed: 49 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -440,78 +440,81 @@ The important takeaway here is with the package.json file and the compiled JavaS
440
440
441
441
<ShowIfrs>
442
442
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:
444
444
445
445
```lf-c
446
446
target Rust;
447
447
448
-
main reactor {
448
+
main reactor {
449
449
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
+
}
451
458
=}
452
459
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
+
}
456
464
}
457
465
```
458
-
459
-
This will print:
460
-
466
+
This will print:
461
467
```
462
-
3.141592653589793
468
+
add macro example: 2
469
+
add 42 example: 52
463
470
```
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
-
};
471
471
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:
475
473
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
+
=}
481
481
482
+
reaction(startup) {=
483
+
println!("{}", PI);
484
+
=}
485
+
}
482
486
```
483
487
This will print:
484
488
```
485
-
Hello, world!
489
+
3.141592653589793
486
490
```
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.
487
492
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:
0 commit comments