Hello.
I need to get the name of an enum::struct instance.
pub enum Node {
IntNode {value: i32},
FloatNode {value: f32}
}
let e = Node::IntNode {value: 100};
let name = somefunctionname(e); // Something like `getStructName(e)`
name // Should return a String or &str with the value `IntNode`
Then I need to get a method by that string.
impl Interpreter {
fn visit(&mut self, funcname: String) {
let funcname = format!('visit_{}', funcname);
let method = somefunctionname(funcname); // Something like `getMethod(funcname, self)`
method(); // If funcname is `visit_IntNode` then this is equivalent to `self.visit_IntNode();`
}
fn visit_IntNode(&mut self) {}
}
If one of these is not possible, please let me know of a good alternative.
This sort of dynamic runtime reflection isn't possible in rust. Why do you want to do such a thing?
As you're always going to be choosing functions from a fixed set (as you cant add named functions at runtime), so why not use a enum to represent all posibilitys, and match on it to decide which function to call.
This looks like what you want. Stringly typed API's are almost always bad as a string is not very expressive for when you dont want to encode any possible text.
This getattr() pattern, while idiomatic in dynamic languages like Python and Ruby, is often frowned upon by statically typed languages*.
If you are recursively visiting nodes, pass a &Node to the visit() method and then use a match statement to run different code depending on the variant. That way you are guaranteed to handle every branch and if nodes get renamed or added over time you'll immediately get a compile error telling you to update your code.
It tends to be quite error-prone - what if I accidentally called my method visit_ItnNode instead of visit_IntNode? What if the value is expecting a string, but I pass it an int?
The API is hard to discover (intellisense can't help you), and
it's easy to introduce bugs when code gets refactored, variants are added, or names are changed