macro_rules! func {
(|$x:ident| $code:expr) => { ... };
($e:expr) => { ... };
($x:ident -> $code:expr) => { ... };
($x:ident [$xt:ty] -> $code:expr) => { ... };
($x:ident [$xt:ty] -> $code:expr => [$yt:ty]) => { ... };
($x:ident -> $code:expr => [$yt:ty]) => { ... };
}
Expand description
A handy macro for creating Function
instances easier.
This macro has three possible syntaxes for defining functions: closure-like, raw and arrow. It casts the created closure to fn($x_type) -> $y_type
to ensure that they have the same type signature (needed for usage in MultiGraph
).
The first of them is just the same syntax as a simple closure, without possible type annotations, as the type is to be set to f64
. On the other hand, you have the raw syntax, which allows basically anything, included a typed closure, but casting to an fn(T) -> U
is required when using MultiGraph
so all functions have the same signature. An example usage is the following:
use tgraph::{MultiGraph, func};
fn main() {
MultiGraph::new_screen(vec![
func!(|x| f64::sin(x/2f64).abs() * 4f64),
func!(|x: f64| -> f64 {x.ln()} as fn(f64) -> f64),
]).draw();
}