This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/_archive/rhai_engine/rhaibook/rust/generic.md
2025-04-04 08:28:07 +02:00

37 lines
1.0 KiB
Markdown

Register a Generic Rust Function
================================
{{#include ../links.md}}
```admonish warning.small "No monomorphization"
Due to its dynamic nature, Rhai cannot monomorphize generic functions automatically.
Monomorphization of generic functions must be performed manually.
```
Rust generic functions can be used in Rhai, but separate instances for each concrete type must be
registered separately.
This essentially _overloads_ the function with different parameter types as Rhai does not natively
support generics but Rhai does support _[function overloading]_.
The example below shows how to register multiple functions (or, in this case, multiple overloaded
versions of the same function) under the same name.
```rust
use std::fmt::Display;
use rhai::Engine;
fn show_it<T: Display>(x: &mut T) {
println!("put up a good show: {x}!");
}
let mut engine = Engine::new();
engine.register_fn("print", show_it::<i64>)
.register_fn("print", show_it::<bool>)
.register_fn("print", show_it::<ImmutableString>);
```