I'm looking to set up a simple macro with the following form:
macro_rules! constants {
($type: ty, $name: ident) => {
pub(crate) mod $name {
pub(crate) const PI: $type = std::$name::consts::PI;
}
}
}
It is pretty nice and straightforward. But is there a way to assign $name
based on the input $type
(or vice versa). It would be nice to not require the same text input to be written twice, getting constants!(f64, f64);
to reduce to just constants(f64);
?
I think the next step would be allowing for inputting of a series of types, so constants!(f32, f64);
But the best I can get to so far is:
macro_rules! constants {
( $( $type:ty, $name:ident ),* ) => {
$(
pub(crate) mod $name {
pub(crate) const PI: $type = std::$name::consts::PI;
}
)*
}
}
constants!(f32, f32, f64, f64);