Macro hygiene makes it hard to insert snippets from metaparameters
Here's some example code that I would have expected to work, based on something I ran into while working on my derive-builder clone:
pub use derive_deftly;
use derive_deftly::{define_derive_deftly,Deftly};
define_derive_deftly! {
pub Accessors =
impl $ttype {
$(
pub fn $<get_ $fname>(&self) -> &$ftype {
${if fmeta(get_with) {
${fmeta(get_with)}
} else {
&self.$fname
}}
}
)
}
}
#[derive(Deftly)]
#[derive_deftly(Accessors)]
pub struct MyStruct {
this: u32,
that: String,
#[deftly(get_with="&self.the_other")]
the_other: u8,
}
When I try to build this, I get:
error[E0424]: expected value, found module `self`
--> src/lib.rs:25:23
|
9 | pub fn $<get_ $fname>(&self) -> &$ftype {
| ----------------------------------------- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters
...
25 | #[deftly(get_with="&self.the_other")]
| ^^^^^^^^^^^^^^^^^ `self` value is a keyword only available in methods with a `self` parameter
I would have expected that this 'fmeta' string would have been inserted verbatim.