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
If I write a macro intended to generate new functions at compile time, I am able to name the functions according to individual idents, but I cannot annotate or concatenate anything onto the ident if I wish.
Consider the code below:
#[macro_export]macro_rules! components {( $($cfield:ident => $component:ident),+ ) => {pubstructComponentManager{
$(pub $cfield:ComponentMap<$component>,)+
}implComponentManager{pubfn new() -> ComponentManager{use std::collections::HashMap;ComponentManager{
$($cfield:HashMap::new(),)+
}}// This below is the code in question!
$(pubfn $cfield(&self){
println!("{}",self.$cfield.len());// Placeholder})+
pubfn delete_all_belonging_to(&mutself, id:EntityId){
$(self.$cfield.remove(&id);)+
}}};}
then the ComponentManager now has a positions(&self) method. But if I wanted the method to be called print_positions(&self), and in the macro I were to try to implement the code in question like this:
// This below is the code in question!
$(
pub fn print_$cfield(&self){println!("{}",self.$cfield.len());// Placeholder})+
then I am presented with the following error:
ecs/mod.rs:53:27: 53:34 error: expected one of `(` or `<`, found `light`
ecs/mod.rs:53 pub fn print_$cfield(&self) {
^~~~~~~
ecs/mod.rs:53:27: 53:34 error: expected one of `(` or `<`, found `light`
ecs/mod.rs:53 pub fn print_$cfield(&self) {
^~~~~~~
Build failed, waiting for other jobs to finish...
Could not compile `project`.
TL; DR: When using macros to generate function names, I am forced to use individual identifiers and cannot prefix/suffix them with other text. Is this behavior a bug in my code, a bug in rustc, or by design?
The text was updated successfully, but these errors were encountered:
If I write a macro intended to generate new functions at compile time, I am able to name the functions according to individual
ident
s, but I cannot annotate or concatenate anything onto theident
if I wish.Consider the code below:
Assuming that I write the following:
then the ComponentManager now has a
positions(&self)
method. But if I wanted the method to be calledprint_positions(&self)
, and in the macro I were to try to implement the code in question like this:then I am presented with the following error:
TL; DR: When using macros to generate function names, I am forced to use individual identifiers and cannot prefix/suffix them with other text. Is this behavior a bug in my code, a bug in
rustc
, or by design?The text was updated successfully, but these errors were encountered: